python类似sizeof,Python“ sys.getsizeof”从列表/字典中删除项目后报告相同的大小?...

I notice that when using sys.getsizeof() to check the size of list and dictionary, something interesting happens.

i have:

a = [1,2,3,4,5]

with the size of 56 bytes (and empty list has size of 36, so it makes sense because 20/5 = 4)

however, after I remove all the items in the list (using .remove or del), the size is still 56. This is strange to me. Shouldn't the size be back to 36?

Any explanation?

解决方案

The list doesn't promise to release memory when you remove elements. Lists are over-allocated, which is how they can have amortized O(1) performance for appending elements.

Details of the time performance of the data structures: http://wiki.python.org/moin/TimeComplexity

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值