列表中元组的交集 - python [英] intersection of tuples in a list - python
本文介绍了列表中元组的交集 - python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的元组列表:
I have a list of tuples like this :
all_tuples=[(92, 242),(355, 403),(355, 436),(355, 489),(403, 436),(436, 489),(515, 517),(517, 859),(634, 775),(701, 859),(775, 859)]
我需要取所有元组的交集并将它们联合起来.
and I need to take the intersection of all tuples and union them.
The desired result = [{92, 242},{355, 403,436,489},{515, 517,859,701,775,634}]
即相交的元组迭代地联合.
That is the intersected tuples are union iteratively.
我尝试将元组转换为集合,然后求交集,但没有奏效.任何想法?
I tried to convert the tuples to sets and then take the intersection but did not work. Any idea?
推荐答案
这是网络问题,使用networkx
import networkx as nx
G=nx.Graph()
all_tuples=[(92, 242),(355, 403),(355, 436),(355, 489),(403, 436),(436, 489),(515, 517),(517, 859),(634, 775),(701, 859),(775, 859)]
G.add_edges_from(all_tuples)
list(nx.connected_components(G))
Out[1216]: [{92, 242}, {355, 403, 436, 489}, {515, 517, 634, 701, 775, 859}]
这篇关于列表中元组的交集 - python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文