I'm using a dictionary to hold a large number of objects, and have a string name for each of them. To be specific, here is my code:
from itertools import product
for (i,j,k) in product(range(N),range(M),range(K)):
var_name='x_'+'_'+str(i)+str(j)+'_'+str(k)
var_dict[var_name] = f(var_name,other_params)
print len(var_dict)
f(...)
returns an object. In my code N=363, M=500, and K=2. So I expect 363000 entries in the dictionary. But when I check the length of var_dict
, it is 330860!
(Pdb) len(var_dict)
330860
Here are my questions:
Is there any explanation for that? E.g. is there any limit for the number of items that built-in hash table of python can address?
What can I do to solve this problem?
'x_'+'_'+str(i)+str(j)+'_'+str(k)
makes no sense as there would be no reason to use two consecutive string literals, and it would be strange to specify a double underscore followed by two variable quantities. Surely OP meant to write'x_'+str(i)+'_'+str(j)+'_'+str(k)
instead, although of course there are better ways to format a string. – Sessions