Maximum dictionary size in Python?

I use the python dictionary to store a large number of objects and there is a line name for each of them. To be specific here, my code is:

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. Therefore, I expect 363,000 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: 1) Are there any explanations for this? For instance. Is there any limit to the number of elements that the python built-in hash table can address?

2) What can I do to solve this problem?

Thank!

+4
source share
3 answers

The problem is here:

str(i)+str(j)

. , , , i=1 j=11 , i=11 j=1 ( ).

, (, , , j k).

+11

i j , (12, 1, 0) (1, 21, 0) . , ; :

var_dict[i, j, k] = f(i, j, k, other_params)

f , , i j:

var_name = 'x_{}_{}_{}'.format(i, j, k)

, , , f :

var_dict[i, j, k] = f(var_name, other_params)
+8

d = {}
for i in xrange(999999):
    d[i] = i
len(d)

999999
+3
source

Source: https://habr.com/ru/post/1539691/


All Articles