Oct-25-2016, 04:30 PM
Given the following, how can I best create a dictionary as follows:
1 [2] 2 [3, 4] 3 [1] 4 [4, 5]The sample code I have thus far is:
#!/usr/bin/python3 from collections import namedtuple Edge = namedtuple("Edge", "v1 v2") V = [Edge(1,2), Edge(2,3), Edge(3,1), Edge(4,1), Edge(2,4), Edge(4,5)] Graph = {} # Graph = {x.v1 for x in V [for y in V } for x in V: for y in V[x]: Graph[x.v1] = [y]I get the following error however:
for y in V[x]: TypeError: list indices must be integers, not Edge