我需要从文件中查找循环,该文件的组织方式如下:
28 36
17 43
36 28
43 69
69 17
8 94
在示例中,我想将 [28, 36]
标识为一个列表,因为我有 28 -> 36
,然后是 36 -> 28
。出于同样的原因,将 [17, 43, 69]
作为另一个列表。如果循环不闭合,比如上面的8和94,我就不需要它们。
Python 的方法是什么?
最佳答案
这是一个有趣的non trivial任务。不管怎样,Python 的方式是使用一个库。这是使用 networkx 的解决方案
import networkx as nx
from networkx.algorithms.cycles import simple_cycles
g = nx.DiGraph()
g.add_edges_from([(28, 36), (17, 43), (36, 28), (43, 69), (69, 17), (8, 94)])
print list(simple_cycles(g))
输出
[[28, 36], [69, 17, 43]]
关于python - 搜索链接号码的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014153/