gpt4 book ai didi

python - 为什么这个包含 7 项的字典只占用 368 个字节?

转载 作者:太空狗 更新时间:2023-10-29 20:26:55 25 4
gpt4 key购买 nike

在深入阅读之后,所有文档都会说明关于字典的两件事:

  • 他们以足够的容量实例化“8”个项目
  • 他们隐式调整大小为 2/3 满(50,000 项以下 4 倍,以上 2 倍)

如果是这样,为什么这个字典只占用 368 字节的 RAM,而空字典占用 240 字节,难道不应该调整大小为 4 倍,例如:960 字节吗?

    >>> getsizeof(dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7))
368
>>> getsizeof(dict(a=1,b=2,c=3))
240

我是否被误导或误解了这里的核心内容?关于 python 3.7 的这些信息有什么变化吗?

最佳答案

import sys
dictt=dict()
array=[]
for i in range(0,1000):
dictt[i]=i
array.append(sys.getsizeof(dictt))
print(array)

numberarray=[]
for i in range(1,1001):
numberarray.append(i)

import matplotlib.pyplot as plt
plt.plot(numberarray,array)
plt.ylabel('memory')
plt.xlabel("items")
plt.show()

enter image description here

现在形象化这个问题有很大帮助。你有一个很好的辩论话题。 enter image description here

你应该引用: http://www.jessicayung.com/how-python-implements-dictionaries/

关于python - 为什么这个包含 7 项的字典只占用 368 个字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55926646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com