I want to store a large dictionary using shelve. More specifically, what I have is rather a dictionary of dictionaries, like a matrix:
dict[key1][key2] = value
The number of keys is around 5.000, which makes a matrix of 5.000x5.000 elements, impossible to store in my 4Gb of memory. For that reason I thought shelve may be a good solution.
Nevertheless, I haven't found any documentation about how to build a dictionary of dictionaries efficiently.
So far, what I have done is to use shelve to create a dictionary that contains regular dictionaries:
def create_entry(key1,key2,value,shelf): # shelf is the opened shelve file
if key1 not in shelf: # first time we see this key, initialise it
shelf[key1]={}
# add the contents
shelf[key1][key2]=value
That works, but it seems to be that there should be a better way to do that taking full advantage of shelve. Any ideas?