3

我想在 python 中获取已填充字典的大小。我试过这个:

>>> dict = {'1':1}
>>> import sys
>>> print dict
{'1': 1}
>>> sys.getsizeof(dict)
140

但这显然不会这样做。我期望的返回值为 2(字节)。我会有一本字典,内容如下:

{'L\xa3\x93': '\x15\x015\x02\x00\x00\x00\x01\x02\x02\x04\x1f\x01=\x00\x9d\x00^\x00e\x04\x00\x0b', '\\\xe7\xe6': '\x15\x01=\x02\x00\x00\x00\x01\x02\x02\x04\x1f\x01B\x00\xa1\x00_\x00c\x04\x02\x17', '\\\xe8"': '\x15\xff\x1d\x02\x00\x00\x00\x01\x02\x02\x04\x1f\x01:\x00\x98\x00Z\x00_\x04\x02\x0b', '\\\xe6@': '\x15\x014\x02\x00\x00\x00\x01\x02\x02\x04\x1f\x01@\x00\x9c\x00\\\x00b\x04\x00\x0b'}

我想知道我需要发送多少字节的数据。我的索引是 6 字节,但内容有多长?我知道这里每个索引是 46Bytes,所以我想知道我需要传输 4*(6+46) Bytes.... 我怎样才能做到最好?

谢谢,罗恩

4

1 回答 1

2

那么,当我需要逐字节传输内容时,只有这给了我真正的长度吗?

#non_mem_macs is my dictionary
for idx in non_mem_macs:
    non_mem_macs_len += len(hexlify(idx))
    non_mem_macs_len += len(hexlify(non_mem_macs[idx]))
于 2012-11-09T21:49:15.187 回答