Viv*_*ble 4 python string dictionary list
我有以下格式的字典:
演示代码:
>>> 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
>>>
Run Code Online (Sandbox Code Playgroud)
我无法得到嵌套字典如何存储在内存中,因为大小if data
为136字节,大小data["sequence"]
为80字节,大小data["lookup"]
为520字节.
当我从可变数据类型转换dictionary
到string
那时字符串变量的大小是3587 bytes
.
演示代码:
>>> data_str = str(data)
>>> sys.getsizeof(data_str)
3587
Run Code Online (Sandbox Code Playgroud)
可以解释一下为什么吗?
字典和列表存储引用(与Python中的每个其他标准容器一样).sys.getsizeof()
不遵循引用,它给你的C结构的内存占用唯一.引用是C指针; 它们的大小取决于您的特定平台.
将字典转换为repr()
字符串也会递归地将内容转换为()字符串,因此所有这些引用都会被解引用并包含在输出中.请注意,这不是原始对象的内存大小的准确反映; 字符串包含字符,它取决于您的确切Python版本,操作系统和Unicode代码点的范围,使用每个字符占用的内存量,字符数与被反射的实际对象具有非线性关系.
如果你想知道一个字典的内存占用,你需要递归地执行这些内容.考虑到字典可以包含对自身的引用(直接或间接),或者任何对象可以对其进行多次引用,并且只应计数一次.我将使用该id()
函数来跟踪已处理的对象.
Stack Overflow上已经有几篇帖子讨论使用递归或其他工具计算容器的内存大小,请参阅深入版本的sys.getsizeof,Python深度获取内容列表?,以及Python中字典的内存使用情况?举个例子.