I have a huge dictionary something like this:
d[id1][id2] = value
example:
books["auth1"]["humor"] = 20
books["auth1"]["action"] = 30
books["auth2"]["comedy"] = 20
and so on..
Each of the “auth” keys can have any set of “genres” associated wtih them. The value for a keyed item is the number of books they wrote.
Now what I want is to convert it in a form of matrix…something like:
"humor" "action" "comedy"
"auth1" 20 30 0
"auth2" 0 0 20
How do i do this?
Thanks
Use a list comprehension to turn a dict into a list of lists and/or a numpy array:
EDIT
Apparently you have an irregular number of keys in each sub-dictionary. Make a list of all the genres:
And then iterate over the dictionaries in the normal manner:
Basically I’m attempting to append a value from each key in
genresto a list. If the key is not there, it throws an error. I catch the error, and append a 0 to the list instead.