Efficiently Querying two large dictionaries stored as pickle files

Viewed 174

I have two large pickle files. Both of them have a one dictionary stored in them. I can not load both pickle files at the same time in to the memory, however, I want to merge the dictionaries stored in them and query on this merged dictionary. Is it possible to do this? See following snippets to get more insights in my query.

f1 = open('d1.pickle', 'rb')
d1 = pickle.load(f1)

# However, I can not load a second dictionary as follows in the same program
# because of memory limits

f2 = open('d2.pickle', 'rb')
d2 = pickle.load(f2)

f3 = open('d_final.pickle', 'wb')
pickle.dump({*d1, *d2}, f3)

UPDATE : I somehow want to do a faster querying on both of these dictionaries. One of my approach was to store both of these dictionaries in a single tsv file and then store the line offsets as indices of keys. Using these offsets, we can seek in that file position and get the value. But this seems a far slower than querying on the dictionary. See following for my attempt. Can anyone comment on whether or not this is the fastest possible way to query both the dictionaries?

f = open('merged_dict.tsv', 'wt')

f1 = open('d1.pickle', 'rb')
d1 = pickle.load(f1)

offsets = {}
for key in d1:
    offset = f.tell()
    f.write(key + '\t' + d1[key] + '\n')
    offsets[key] = offset

del f1, d1

f2 = open('d2.tsv', 'rb')
d2 = pickle.load(f2)

for key in d2:
    offset = f.tell()
    f.write(key + '\t' + d2[key] + '\n')
    offsets[key] = offset

f3 = open('dict_offets.pickle', 'wb')
pickle.dump(offsets, f3)

Now when I need to query the dictionary, I do following :

f = open('merged_dict.tsv', 'rt')
f1 = open('dict_offsets.pickle', 'rt')

d = pickle.load(f1)

#query the dictionary using following function. 
def query_dictionary(key):
    offset = d[key]
    f.seek(offset)
    return f.readline().split('\t')[1]

I think the performance issue with storing dictionaries as tsv file is because of the values being numpy arrays. I need to convert strings to numpy arrays, which might be impacting the querying performance.

If this is not the best way, someone please suggest the better approach for this. Once again, I'm highly concerned with the efficiency of querying. I want to get the value from dictionary as fast as possible.

0 Answers
Related