intersection of tuples in a list - python

后端 未结 3 1061
耶瑟儿~
耶瑟儿~ 2020-12-21 08:35

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)         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-21 08:58

    This is network problem , using 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}]
    

提交回复
热议问题