#ideal nodes list should be
['A','B','C','D','E','F','G','H','I','J','K','L','M','N']
所以我尝试编写一个定义来读取节点和边。这是我的代码,但它似乎不起作用。
""" read nodes"""
def rd_nodes(a):
nline =[line.split(":")[1].replace(';',',').split(',') for line in a]
for i in nline:
return i
最佳答案
itertools.combinations
可以在这里为您提供帮助。
试试这个:
from itertools import combinations
s = """
1:A,B,C,D;E,F
2:G,H;J,K
&:L,M,N
"""
nodes = set()
edges = set()
for line in s.split():
clusters = line.split(':')[1].split(';')
for cluster in clusters:
_nodes = cluster.split(',')
nodes.update(_nodes)
edges.update(combinations(_nodes, 2))
您可以使用collections.OrderedDict
如果需要,可以使用节点
和边
的结构来维持顺序。只需使用节点和边作为字典键,然后在脚本末尾获取键列表。
关于python - 在 python 中获取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39796301/