Reputation: 4477
I'm trying to get a python dictionary with the following structure:
>>>my_dict
>>>(i1,j1):{c1:1,c2:3,c3:4},(i2,j2):{c1:1,c2:1,c3:1}...
What I would like to do is get the key (i,j) and the r1,r2,r3.rn in a matrix or some sort of table used for visulization. So the table representation would look like this.
c1 c2 c3 ...rN
i1,j1 1 3 4
i2,j2 1 1 1
iN,jN
Basically I want my primary keys on the rows and the keys of my value (which is another dictionary) on the column. Then easily map the keys to a column and row value.
I'm being really specific, and this is just one example. I'm actually confused as to what i'm looking for. I have to do this many times, Is their an easy way to map dictionaries to a table like this? Is there some module I should be looking for?
Thanks in advance, J
Upvotes: 1
Views: 486
Reputation: 251106
dic={('i1','j1'):{'c1':1,'c2':3,'c3':4},('i2','j2'):{'c1':1,'c2':1,'c3':1}}
header=list(max(dic.values()))
print '\t',
print " ".join(sorted(header))
for x in dic:
print ",".join(x)+'\t',
for y in sorted(dic[x]):
print str(dic[x][y])+" ",
print
output:
c1 c2 c3
i1,j1 1 3 4
i2,j2 1 1 1
to write this to a file, try this:
with open('data.txt','w') as f:
dic={('i1','j1'):{'c1':1,'c2':3,'c3':4},('i2','j2'):{'c1':1,'c2':1,'c3':1}}
header=list(max(dic.values()))
f.write('\t')
f.write(" ".join(sorted(header))+'\n')
for x in dic:
f.write(",".join(x)+'\t')
for y in sorted(dic[x]):
f.write(str(dic[x][y])+" ")
f.write('\n')
Upvotes: 2
Reputation: 6295
{
'primary_key1': [{key: data},{key: data},{key: data},{key: data},{key: data},{key: data}]},
'primary_key2': [{key: data},{key: data},{key: data},{key: data},{key: data},{key: data}]}
...
'primary_keyN': [{key: data},{key: data},{key: data},{key: data},{key: data},{key: data}]}
}
So Now you can retrieve them back using PK and if you have a keys you can store them in dict with duplicate Keys like :
'primary_key1': [{key: data},{key: data},{key: data},{key: data},{key: data},{key: data}]
or else if you hava always unique key for value then do nesting:
'primary_key1': {key: data, key: data, key: data},
or Else only data then :
'primary_key1': [data, data, data],
Hope this will help you.
Upvotes: 1