Difficulty teaming up neighbours of each cone

前端 未结 2 1552
迷失自我
迷失自我 2021-01-23 22:39

I have a nested list of junctions between cones.

a = [0,1]
b = [2,4]
c = [2,0]
d = [4,3]

e=[a,b,c,d]

I want to write a program that lists eve

2条回答
  •  借酒劲吻你
    2021-01-23 23:22

    I recommend using a dictionary instead.

    a = [0, 1]
    b = [2, 4]
    c = [2, 0]
    d = [4, 3]
    
    e = [a, b, c, d]
    neighbour_list = {}
    
    for x, y in e:
        neighbour_list.setdefault(x, [])
        neighbour_list[x].append(y)
        neighbour_list.setdefault(y, [])
        neighbour_list[y].append(x)
    neighbour_list = list(neighbour_list.values())
    print(neighbour_list)
    

    Running it gives:

    [[1, 2], [0], [4, 0], [4], [2, 3]]
    

提交回复
热议问题