将共享元素的元组分组到单独的列表中

Grouping Tuples that Share Elements into Separate Lists

给定这组数据

[ (1,2),(2,3),(3,4),(7,8),(8,9),(8,11),(12,19) ]

我正在努力达到这个结果

 [ [(1,2),(2,3),(3,4)] , [(7,8),(8,9),(8,11)] , [(12,19)] ]

结果的顺序无关紧要。

我想做的是将 'linked' 的元素分组到单独的列表中。

这是我目前所拥有的,但我有点卡住了

for a,b in links:
    result=[(a,b)]
    tmplinks.remove((a,b))
    for c,d in tmplinks:
        if a==c or a==d or b==c or b==d:
            result.append((c,d))

结果应该能够处理字符串,因为我处理的是表示 IP 地址的字符串。

Starting Data
[ ('1.1.1.1','2.2.2.2'),('2.2.2.2','3.3.3.3'),('4.4.4.4','6.6.6.6'),('6.6.6.6','11.11.11.11') ]

Desired Output
[ [ ('1.1.1.1','2.2.2.2'),('2.2.2.2','3.3.3.3') ] , [ ('4.4.4.4','6.6.6.6'),('6.6.6.6','11.11.11.11') ] ]


1.1.1.1
|
2.2.2.2
|
3.3.3.3


4.4.4.4
|
6.6.6.6
|
11.11.11.11

尝试以下操作:

[lst[i:i+3] for i in range(0, len(lst), 3)]

>>> lst = [ (1,2),(2,3),(3,4),(7,8),(8,9),(8,11),(12,19) ]
>>> [lst[i:i+3] for i in range(0, len(lst), 3)]
[[(1, 2), (2, 3), (3, 4)], [(7, 8), (8, 9), (8, 11)], [(12, 19)]]
>>> 

试试这个。

input1 = [ (1,2),(2,3),(7,8),(3,4),(8,9),(8,11), (12,19)]
temp_input = input1[:]

result_list = []
for a,b in input1:
    if (a,b) in temp_input :
        result=[(a,b)]
        temp_input.remove( (a,b) )
        result_list.append( result )
    for c,d in temp_input :
        if a==c or a==d or b==c or b==d:
            result.append( (c,d) )
            temp_input.remove( (c,d) )
print result_list

你可以使用这个 k-mean 聚类算法。您可以在 python.

中看到此 link 用于 K-mean 实施
N.B:need must be told how many group you need?

你也可以看看A pure python implementation of K-Means clustering.

我想这会对你有所帮助。

您的要求有一些变化(例如,一个项目可以插入任何匹配的组中,或者仅当它与最后一个组的最后一个元素匹配时插入),反之亦然,但我以您的示例为导向,并且结果是这样的:

def group(items):
  iterator = iter(items)
  group = [next(iterator)]
  result =[group]
  for item in iterator:
    (a, b), (c, d) = item, group[-1]
    if a in (c, d) or b in (c, d):
      group.append(item)
    else:
      group = [item]
      result.append(group)
  return result

print(group([(1,2),(2,3),(3,4),(7,8),(8,9),(2,1),(2,3),(1,4)]))

输出:[[(1, 2), (2, 3), (3, 4)], [(7, 8), (8, 9)], [(2, 1), (2, 3)], [(1, 4)]]Ideone

这不会保留边上的顺序,但可以很容易地找到聚类:

nums = [ (1,2),(2,3),(3,4),(7,8),(8,9),(8,11),(12,19) ]
ips = [ ('1.1.1.1','2.2.2.2'),('2.2.2.2','3.3.3.3'),('4.4.4.4','6.6.6.6'),('6.6.6.6','11.11.11.11') ]

import networkx as nx


for testdata in (nums, ips):
    G = nx.Graph()
    G.add_edges_from(testdata)
    sub_graphs = nx.connected_component_subgraphs(G)

    # you can now loop through all nodes in each sub graph
    for s in sub_graphs:
     print(s.edges())
[(1, 2), (2, 3), (3, 4)]
[(8, 9), (8, 11), (8, 7)]
[(19, 12)]

[('11.11.11.11', '6.6.6.6'), ('4.4.4.4', '6.6.6.6')]
[('3.3.3.3', '2.2.2.2'), ('1.1.1.1', '2.2.2.2')]