gpt4 book ai didi

python - 在 python 中获取列表

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:55 26 4
gpt4 key购买 nike

#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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com