【发布时间】:2016-05-22 04:43:31
【问题描述】:
我有以下格式的字典:
演示代码:
>>> import pprint
>>> pprint.pprint(data)
{'lookup': {'F01': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F02': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F03': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F04': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F05': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F06': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F07': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F08': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F09': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F10': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F11': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }',
'F12': '\n.custom1 {\n background-color: #f5e9dc;\n padding: 10px;\n border-radius: 10px;\n font-family: sans-serif;\n font-size: 0.9em;\n margin-top: 1em;\n }\n.custom2 .style8-rw {\n font-family: sans-serif;\n font-weight: bold;\n color: #F57215;\n }'},
'sequence': ['F01',
'F02',
'F03',
'F04',
'F05',
'F06',
'F07',
'F08',
'F09',
'F10',
'F11',
'F12']}
>>> import sys
>>> sys.getsizeof(data)
136
>>> sys.getsizeof(data["sequence"])
80
>>> sys.getsizeof(data["lookup"])
520
>>>
我无法知道嵌套字典是如何存储在内存中的,因为如果 data 的大小为 136 字节,data["sequence"] 的大小为 80 字节,data["lookup"] 的大小为 520 字节。
此外,当我将变量数据从dictionary 类型转换为string 时,字符串变量的大小为3587 bytes。
演示代码:
>>> data_str = str(data)
>>> sys.getsizeof(data_str)
3587
谁能解释一下为什么?
【问题讨论】:
标签: python string list dictionary