Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating a dictionary
#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


Messages In This Thread
creating a dictionary - by bluefrog - Oct-25-2016, 04:30 PM
RE: creating a dictionary - by metulburr - Oct-25-2016, 04:51 PM
RE: creating a dictionary - by ichabod801 - Oct-25-2016, 05:06 PM
RE: creating a dictionary - by bluefrog - Oct-25-2016, 06:08 PM
RE: creating a dictionary - by Yoriz - Oct-25-2016, 06:19 PM
RE: creating a dictionary - by nilamo - Oct-25-2016, 06:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,988 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Creating a dictionary from a list Inkanus 5 3,249 Nov-06-2020, 06:11 PM
Last Post: DeaD_EyE
  Creating Dictionary form LOG /text file DG1234 7 5,643 Feb-13-2019, 08:08 PM
Last Post: DG1234
  creating a username and pword program using a #def statement and #dictionary zcode12 3 3,225 Oct-14-2018, 04:41 AM
Last Post: volcano63
  Creating dictionary from list kerzol81 4 4,446 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