Python聚合共享公共项的列表



我正在寻找一个函数来聚合有一个共同项目的列表。我想到的具体示例如下:

inputs = [['a','b'], ['a','c'], ['b','d'], ['e','f'], ['g','h'], ['i','k'], ['k','l']]
aggregated_output = [['a','b','c','d'],['e','f'],['g','h'],['i','k','l']]
如您所见,所有共享一个公共项的列表都聚集在一起。输出中的列表或列表中的项的顺序不重要。

也许蛮力解决方案可以帮助您:

inputs = [['a','b'], ['a','c'], ['b','d'], ['e','f'], ['g','h'], ['i','k'], ['k','l']]
res = []
for arr in inputs:
flaq = False
for r in res:
for a in arr:
if a in r:
r += [a for a in arr if not a in r]
flaq = True
break
if not flaq:
res.append(arr)
print(res)

输出:

[['a', 'b', 'c', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'k', 'l']]

您可以使用networkx包中的connected_components:

>>> import networkx as nx
>>> edges = [['a', 'b'], ['a', 'c'], ['b', 'd'], ['e', 'f'], ['g', 'h'], ['i', 'k'], ['k', 'l']]
>>> graph = nx.Graph()
>>> graph.add_edges_from(edges)
>>> [list(c) for c in nx.connected_components(graph)]
[['a', 'c', 'd', 'b'], ['f', 'e'], ['h', 'g'], ['k', 'i', 'l']]

最新更新