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
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]]