Multilevel Dictionary In Python
I would like to create a perl style multilevel dict in python however I'm struggling to get this going. This is what I have tried: import sys import csv import pprint from collecti
Solution 1:
You're actually looking for a tree structure. There's a simply Python function that provides this structure:
from collections import defaultdict
deftree():
return defaultdict(tree)
Now you can set it as follows:
hash = tree()
hash['Center']['ROOM1']['EXAM1'] = 1
hash['Center']['ROOM1']['EXAM2'] = 2
hash['Center']['ROOM2']['EXAM1'] = 3
hash['Center']['ROOM2']['EXAM2'] = 4
hash['Center']['ROOM2']['EXAM3'] = 5
hash['Center2']['ROOM3']['EXAM1'] = 6
You can convert these back to dicts using:
defdicts(tree):
return {key: (dicts(tree[key]) ifhasattr(tree[key], 'items') else tree[key]) for key in tree}
For example, here's a prettified output for the hash
variable above:
>>> import json
>>> print json.dumps(dicts(hash), indent=4)
{
"Center2": {
"ROOM3": {
"EXAM1": 6
}
},
"Center": {
"ROOM2": {
"EXAM2": 4,
"EXAM3": 5,
"EXAM1": 3
},
"ROOM1": {
"EXAM2": 2,
"EXAM1": 1
}
}
}
Solution 2:
I am not familiar with pearl, but in python you need to initiate a dictionary before calling a specific entry. The way you are using defaultdict, you create a structure that is only two levels deep.
If you don't really need the defaultdict functionality, this is a not so elegant, but fast way to do what you want.
import sys
import csv
import pprint
hash = {}
FILE = csv.reader(open('dosageMP.txt', 'rU'), delimiter='\t')
FILE.next()
count =0for row in FILE:
if count <10:
print row
hash[row[0]]={}
hash[row[0]][row[10]]={}
hash[row[0]][row[10]][row[5]]=1
count = count+1
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(hash)
I am not familiar with pprint either, but I hope it can handle this structure.
Post a Comment for "Multilevel Dictionary In Python"