嵌套字典和列表的大小 [英] size of nested dictionary and list

查看:104
本文介绍了嵌套字典和列表的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的字典:

演示代码:

>>> 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
>>> 

我无法得到嵌套字典如何存储在内存中,因为大小如果数据是136字节,大小 data [sequence] 是80字节,也是 data [lookup ] 是520字节。

I can not get how nested dictionary is store in memory because size if data is 136 bytes and size of data["sequence"] is 80 bytes and also size of data["lookup"] is 520 bytes.

当我从字典 string 然后字符串变量的大小是 3587字节

Also when I do type casting of variable data from dictionary to string then size of string variable is 3587 bytes.

演示代码:

>>> data_str = str(data)
>>> sys.getsizeof(data_str)
3587

可以任何解释我为什么? >

Can any on explain me why?

推荐答案

字典和列表存储引用(像Python中的所有其他标准容器)。 sys.getsizeof()不遵循引用,它只提供C结构的内存占用。引用是C指针;将它们的大小取决于你的具体平台。

Dictionaries and lists store references (like every other standard container in Python). sys.getsizeof() doesn't follow references, it gives you the memory footprint of the C structure only. The references are C pointers; their size depends on your specific platform.

将字典转换为字符串将内容转换为( repr())字符串,所以所有这些引用然后被取消引用并包含在输出中。请注意,这不是原始对象的内存大小的准确反映;字符串包含字符,这取决于您确切的Python版本,操作系统和Unicode码点的范围,使用了每个字符占用多少内存,并且字符数与反映的实际对象具有非线性关系。

Converting a dictionary to a string recursively converts the contents to (repr()) strings too, so all those references are then dereferenced and included in the output. Note that this is not an accurate reflection of the memory size for the original object; strings contain characters, it depends on your exact Python version, OS and range of Unicode codepoints used how much memory each character takes, and the character count has a non-linear relationship to the actual object being reflected.

如果您想知道内容的字典的内存占用,则需要递归地执行此操作。考虑到字典可以包含对其自身(直接或间接)的引用,或者任何对象可以有多个引用,并且只能被计入一次。我会使用 id()函数来跟踪哪些对象已被处理。

If you want to know the memory footprint of a dictionary with the contents you need to do so recursively. Take into account that a dictionary can contain references to itself (directly or indirectly), or that any object can have multiple references to it and should only be counted once. I'd use the id() function to track what objects have already been handled.

Stack Overflow中已经有几个帖子讨论了使用递归或其他方法计算容器的内存大小工具,请参阅深度版本的sys.getsizeof 具有内容的Python深度getizeof列表,而在Python中的字典的内存使用情况为例。

There are already several posts on Stack Overflow that discuss calculating memory size for containers using recursion or other tools, see Deep version of sys.getsizeof, Python deep getsizeof list with contents?, and Memory-usage of dictionary in Python? for some examples.

这篇关于嵌套字典和列表的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆