当您试图将所有组合转换为列表时,您的程序很可能会崩溃: print(len(list(combinations(G.nodes, 3)))) 是的。永远不要做因为 combinations 返回一个迭代器,该迭代器消耗少量内存,但list很容易占用千兆字节的内存。
print(len(list(combinations(G.nodes, 3))))
combinations
如果有稀疏图,则在 connected components 以下内容: nx.connected_components(G)
nx.connected_components(G)
NetworkX有 triads 子模块,但看起来不适合你。我已经修改了networkx.algorithms.triads代码以返回triads,而不是它们的计数。你可以找到它 here 是的。注意它使用有向图。如果要将它与无向图一起使用,应该先将它们转换为有向图。
看起来您只是想用2个或更多的空格拆分字符串。你可以这样做:
s = "a b d d c" re.split(r' {2,}', s)
将返回您:
['a', 'b d d', 'c']