小编Eki*_*ksu的帖子

洗牌后的结果不同

我想将一个不完整的图分成单独的,未连接的体.图表的边缘位于列表中edges.

代码在改组边的顺序时给出不同的结果.这是为什么?

from random import shuffle

edges = [('7', '9'), ('2', '8'), ('4', '10'), ('5', '9'), ('1', '2'), ('1', '6'), ('6', '10')]
bodylist = []
shuffle(edges)

for edge in edges:
    #If at least one node of the edge is anywhere in bodylist, append the new nodes to that list.
    try:
        index = [i for i, body in enumerate(bodylist) if edge[0] in body or edge[1] in body][0]
        bodylist[index].append(edge[0])
        bodylist[index].append(edge[1])
    #If not, make a new list containing the new nodes. …
Run Code Online (Sandbox Code Playgroud)

python algorithm shuffle graph python-3.x

5
推荐指数
1
解决办法
101
查看次数

标签 统计

algorithm ×1

graph ×1

python ×1

python-3.x ×1

shuffle ×1