Python 字典大小最大限制

Python Dictionary Size Maximum Limit

我正在尝试了解以下内存错误的原因。 python 中的字典是否有一些预定义的限制?

self.text 是从文件中读入的长字符串(大约 4.5 MB) L等于4641652

    L = len(self.text) 
    test = {}
    for i in xrange(L,0,-1):
        try:
            test[i] = self.text[i-1:]
        except MemoryError:
            print "Memory Error at the " + str(i) +"th iteration!"
            print sys.getsizeof(test)
            print len(test)
            exit()

输出

Memory Error at the 4577890th iteration!
1573004
63762

如果有帮助的话,我是 运行 windows 机器上的程序,内存为 16gb。

您正在循环中存储 1 + 2 + 3 + ... + 4641650 + 4641651 + 4641652... 字节。通过所讨论的迭代,您已经进行了大约 63762 次,即 2032796322 字节。再多一条双线,你瞧,你超过了 32 位整数限制,这对我来说似乎是 运行 进入内存错误的合理位置。