python 集可以包含的值的数量是否有限制? [英] Is there a limit to the number of values that a python set can contain?

查看:46
本文介绍了python 集可以包含的值的数量是否有限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python 集作为来自 mysql 表的 id 的过滤器.python 集存储所有要过滤的 id(现在大约 30 000),这个数字会随着时间的推移缓慢增长,我担心 python 集的最大容量.它可以包含的元素数量有限制吗?

I am trying to use a python set as a filter for ids from a mysql table. The python set stores all the ids to filter (about 30 000 right now) this number will grow slowly over time and I am concerned about the maximum capacity of a python set. Is there a limit to the number of elements it can contain?

推荐答案

您最大的限制是计算机上的内存量.试试这条线:

Your biggest constraint is the amount of memory on your computer. Try the line:

s = set(xrange(10000000))

这会创建一个长度为 1000 万的集合,比您举的示例中的 30,000 大得多.在我的计算机(具有 4GB 内存的 Macbook Air)上,这只需几秒钟即可运行.无论您使用何种系统,您都可能同样不受约束.

That creates a set of length 10 million, much larger than the 30,000 you give as an example. On my computer (a Macbook Air with 4GB of memory) this runs in only a few seconds. Whatever your system, it is probably you will be similarly unconstrained.

当然,绝对有一个上限,在此之前很久,您的 set 会因为冲突的数量以及必要的内存交换而开始变慢.因此,这个集合有多大很重要.如果您将处理超过 1000 万个项目,您可能需要考虑使用数据库.

Of course, there is absolutely an upper limit, and long before that your set will start to slow down because of the number of collisions, as well as the necessary memory swapping. It thus matters how big this set will get. If you will be working with more than 10 million items, you might want to consider working with databases instead.

这篇关于python 集可以包含的值的数量是否有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆