预先声明的字典大小限制?
我有一本大字典,我经常在代码中引用,因此我将其在顶部初始化:
import ...
myDictionary = {'a':'avalue','b':'bvalue',...}
code ...
但是当我尝试获取值时,找不到某些键。由于大小限制,Python 似乎正在砍掉我的字典。我尝试搜索谷歌,但找不到任何关于此的信息。
我最终将键:值映射转储到一个单独的文件中,并编写了一个函数,通过读取该文件来构建字典。
很高兴知道为什么会发生这种情况......甚至更好地找到一种更干净的方式来保留我的字典。
编辑:字典有超过 1,700 个键
I have a big dictionary i constantly reference in my code so i have it initialized at the top:
import ...
myDictionary = {'a':'avalue','b':'bvalue',...}
code ...
But when i try to get values, some of the keys are not found. It appears as though Python is chopping my dictionary due to a size limit. I tried searching google but couldn't find anything on this.
I ended up dumping the key:value mappings into a separate file and wrote a function that would build the dictionary by reading in the file.
It would be nice to know why this is happening... even better to find a cleaner way to still have my dictionary.
EDIT: Dictionary has over 1,700 keys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要查找的一件事是字典中的键不重复。例如,在下面的代码中:
因为我使用了键
'1'
两次,所以只出现了最后一个,因此我留下了一个大小为 2 而不是 3 的字典。One thing you might want to look for is that the keys in your dictionary are not duplicates. For example, in the following code:
because I used the key
'1'
twice, only the last one appeared and thus I was left with a dictionary of size 2 rather than 3.Python 没有字典大小限制。我的字典有超过 100 万个键。您可以发布更多代码吗?
Python does not have a dictionary size limit. I've had dictionaries with well over 1 million keys. Could you post more of the code?