为什么Python中的集合比列表大?

发布于 2024-09-16 06:04:32 字数 261 浏览 2 评论 0原文

为什么 Python 中集合的大小明显大于具有相同元素的列表的大小?

a = set(range(10000))
b = list(range(10000))
print('set size = ', a.__sizeof__())
print('list size = ', b.__sizeof__())

输出:

set size = 524488
list size = 90088

Why is the size of sets in Python noticeably larger than that of lists with same elements?

a = set(range(10000))
b = list(range(10000))
print('set size = ', a.__sizeof__())
print('list size = ', b.__sizeof__())

output:

set size = 524488
list size = 90088

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

记忆里有你的影子 2024-09-23 06:04:32

setlist 使用更多内存,因为它存储所有元素的哈希表,因此可以快速检测重复条目等。这就是为什么每个集合成员都必须可哈希

The set uses more memory than the list as it stores a table of hashes of all the elements so it can quickly detect duplicate entries and so on. This is why every set member must be hashable.

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