Thanks for feedback.
I get the following result after the change you proposed.
1 2
2 4
3 1
4 5
whereas what I would like is
I am attempting to create a list as the value item of the dictionary, whereas the key is the first vertex, i.e. v1. So the result I am after is:
1 [2]
2 [3, 4]
3 [1]
4 [4, 5]
Is it possible to specify something like this?
thanks for that bunny rabbit.
I used your code and it works.
I get the following result after the change you proposed.
1 2
2 4
3 1
4 5
whereas what I would like is
I am attempting to create a list as the value item of the dictionary, whereas the key is the first vertex, i.e. v1. So the result I am after is:
1 [2]
2 [3, 4]
3 [1]
4 [4, 5]
Is it possible to specify something like this?
for x in V: Graph[x.v1][].append(x.v2)I get an error.
thanks for that bunny rabbit.
I used your code and it works.
#!/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 = {edge.v1: [] for edge in V} for edge in V: Graph[edge.v1].append(edge.v2) for x,y in Graph.items(): print(" {} {} ".format(x,y))I'll be expanding on the solution in subsequent posts as I have more questions regarding Graphs in python.