converting a list to dictionary of dictionary in Python

user2015915

I found few advice in this site regarding how to convert a list into a dictionary. I have successfully followed it. But i got struck in performing a conversion from a list to dictionary of dictionary; like graph representation

    I/p: 0,0,1,1,0,4
    o/p: 0:{1:0,2:1}---------------------->1:0 means 1 is vertex and 0 is weight
         1:{0:1,2:4}

Here, keys represent the vertices of graph.

Can you please advice me, how to resole this issue?

THank you

Ofir Israel

First, you need to represent the graph differently. The user input should be a matrix.

Item (i,j) in the matrix should be X if there's an edge weighted X from vertex i to vertex j, and be (let's say) "-1" if there isn't. (This is all interchangeable of course).

    0    1    2

0  -1    0    1

1   1   -1    4

2   1    4   -1

So the input string should be like: -1,0,1;1,-1,4;1,4,-1

Afterwards, use collections.defaultdict (documentation) like so:

>>> d = defaultdict(dict)
>>> input = '-1,0,1;1,-1,4;1,4,-1'
>>> lst = map(lambda x: map(lambda y: int(y), x.split(',')), input.split(';'))
>>> for i,j in enumerate(lst):
...     for k,l in enumerate(j):
...             d[i][k] = l
...
>>> d
defaultdict(<type 'dict'>, {0: {0: -1, 1: 0, 2: 1}, 1: {0: 1, 1: -1, 2: 4}, 2: {
0: 1, 1: 4, 2: -1}})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting a nested list of dictionary in Python

From Dev

Converting list of list to dictionary

From Java

Converting list of lists into a dictionary of dictionaries in Python

From Dev

Converting list of lists into a dictionary of dictionaries in Python

From Dev

Python 2.7 converting list of strings to dictionary

From Dev

Python: converting a list of tuples to dictionary with some conditions

From Dev

Converting Dictionary Value to List

From Dev

Converting a list to a nested dictionary?

From Dev

Converting Dictionary Value to List

From Dev

Converting a List to a Dictionary

From Dev

Converting a List To Dictionary

From Dev

Converting a list of list to list of dictionary

From Dev

converting python string to dictionary

From Dev

converting tuple to dictionary in python

From Dev

Converting to a set dictionary in Python

From Dev

Python - Converting a string into a dictionary

From Dev

Converting a Dictionary into a Dataframe in python

From Dev

converting json to python dictionary

From Java

Converting JSON String to Dictionary Not List

From Dev

Converting CSV data to list in dictionary

From Dev

Converting a distinct list to dictionary not working

From Dev

Converting a list of lists into a dictionary of lists

From Dev

Converting a Dictionary with a hierarchical structure into a List

From Dev

python dictionary(dictionary(dictionary(list))) comparison

From Dev

Python dictionary manipulation into list of dictionary

From Dev

Converting JSON string into Python dictionary

From Dev

python recursive dictionary converting to strings

From Dev

Converting text file to dictionary in python

From Dev

Converting a pandas dataframe into python dictionary

Related Related

HotTag

Archive