将元素分组为单独列表的元组

时间:2015-04-17 04:08:15

标签: python sorting tuples grouping

给定这组数据

[ (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)] ]

结果的顺序无关紧要。

我要做的是将被链接的元素组合在一起'分开列表。

这是我到目前为止所得到的,但我有点卡住了

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

5 个答案:

答案 0 :(得分:2)

这不保留边缘的排序,但它很容易找到簇:

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')]

答案 1 :(得分:0)

试试这个。

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

答案 2 :(得分:0)

你可以使用这个k-mean聚类算法。你可以在python中看到K-mean实现的这个链接。

N.B:need must be told how many group you need?

此外,您可以查看以便更好地了解A pure python implementation of K-Means clustering

我认为这会对你有所帮助。

答案 3 :(得分:0)

您的要求有一些变化(例如,一个项目可以插入任何匹配的组中,或者只有当它与最后一个组的最后一个元素匹配时),反之亦然,但我将自己定位于您的示例并且它来了对此:

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

答案 4 :(得分:-3)

尝试以下方法:

[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)]]
>>>