Python 字典大小最大限制

问题描述 投票:0回答:1

我试图理解以下内存错误的原因。 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

我正在一台具有 16GB RAM 的 Windows 机器上运行该程序,如果有帮助的话。

python dictionary
1个回答
7
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.