为什么Python中的集合比列表大?
为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
set
比list
使用更多内存,因为它存储所有元素的哈希表,因此可以快速检测重复条目等。这就是为什么每个集合成员都必须可哈希
。The
set
uses more memory than thelist
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 behashable
.