Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating a dictionary
#1
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
Reply
#2
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:
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
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.
Reply
#5
#!/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]})
Reply
#6
...?  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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,921 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Creating a dictionary from a list Inkanus 5 3,164 Nov-06-2020, 06:11 PM
Last Post: DeaD_EyE
  Creating Dictionary form LOG /text file DG1234 7 5,465 Feb-13-2019, 08:08 PM
Last Post: DG1234
  creating a username and pword program using a #def statement and #dictionary zcode12 3 3,149 Oct-14-2018, 04:41 AM
Last Post: volcano63
  Creating dictionary from list kerzol81 4 4,345 Oct-23-2017, 11:40 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020