共享常见项目的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']]
如您所见,共享共同项目的所有列表都被捆绑在一起。列表的顺序或输出列表中的项目无关紧要。
I'm looking for a function to aggregate lists that have a common item. The specific example I had in mind was the following case:
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']]
as you can see, all the lists that share a common item have been bunched together. The order of the lists or the items in the lists in the output does not matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许蛮力解决方案为您提供帮助:
输出:
Maybe Brute-Force Solutions help you:
Output:
您可以使用
connected_components
来自 networkx package package package:You could use
connected_components
from the networkx package: