预先声明的字典大小限制?

发布于 2024-09-20 00:52:38 字数 336 浏览 5 评论 0原文

我有一本大字典,我经常在代码中引用,因此我将其在顶部初始化:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧夏天 2024-09-27 00:52:38

您可能想要查找的一件事是字典中的键不重复。例如,在下面的代码中:

>>> d = {'1': 'hello', '2': 'world', '1': 'new'}
>>> d
{'1': 'new', '2': 'world'}
>>> 

因为我使用了键 '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:

>>> d = {'1': 'hello', '2': 'world', '1': 'new'}
>>> d
{'1': 'new', '2': 'world'}
>>> 

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.

梦明 2024-09-27 00:52:38

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文