Posts: 42
Threads: 26
Joined: Oct 2016
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
Posts: 5,151
Threads: 396
Joined: Sep 2016
Oct-25-2016, 04:51 PM
(This post was last modified: Oct-25-2016, 05:08 PM by metulburr.)
Im not really sure what you are trying to accomplish. Are you trying to retain Edge in the dictionary?
for x in V:
Graph[x.v1] = x.v2 Quote:TypeError: list indices must be integers, not Edge
Theres not much else to say. List indices must be an int, whereas you are sending an Edge object. You shoudnt need to do this anyways, as python for loops iterates the objects within. Here x is each Edge object as it iterates through the list. Rarely do you need the index.
Also regarding your naming conventions
http://python-forum.io/Thread-Basic-Nami...ions-PEP-8
Recommended Tutorials:
Posts: 4,220
Threads: 97
Joined: Sep 2016
Oct-25-2016, 05:06 PM
(This post was last modified: Oct-25-2016, 05:10 PM by ichabod801.)
If you want to build a dictionary of lists, you generally start out by creating a dictionary of empty lists:
Graph = {edge.v1: [] for edge in V} The above is a dictionary comprehension that will give you a dictionary with the keys of the first vertices of the Edges, and values of empty lists.
Now you want to go through the edges again and populate those lists. You've got the code to go through the edges:
for edge in V: This gives you the edges. You don't need a second for loop to get at anything. The first time through the loop, edge will be Edge(1, 2). The second time through the loop, edge will be Edge(2, 3). And so on.
Now you just need to add (append) the second vertices (v2) to the lists keyed to the first vertices (v1):
Graph[edge.v1].append(edge.v2) You'll note that we did this going through the list twice: once to get the keys, a second time to populate the lists that are the values. For large datasets, that's inefficient. It's more efficient to go through the list once, getting the keys and the values at the same time. See if you can figure out how to do that.
Posts: 42
Threads: 26
Joined: Oct 2016
Oct-25-2016, 06:08 PM
(This post was last modified: Oct-25-2016, 06:18 PM by bluefrog.)
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?
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.
Posts: 2,168
Threads: 35
Joined: Sep 2016
#!/usr/bin/python3
from collections import namedtuple, defaultdict
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 = defaultdict(list)
for item1, item2 in V:
graph[item1].append(item2)
print(graph) Output: defaultdict(<class 'list'>, {1: [2], 2: [3, 4], 3: [1], 4: [1, 5]})
Posts: 3,458
Threads: 101
Joined: Sep 2016
...? Building dicts isn't hard. And if your keys are all sequentially numerical, you can also use a list. # dict
graph = {
1: [2],
2: [3, 4],
3: [1],
4: [4, 5]
}
# list
other_graph = [
[], # 0
[2], # 1
[3, 4],
[1],
[4, 5]
]
print(graph)
print(other_graph)
|