打印数据结构/数据类型层次结构

时间:2022-10-09 16:29:38

Let's assume we have a Data-Structure which consists of nested Data-Types, is there a way of printing the data-types like:

假设我们有一个由嵌套数据类型组成的数据结构,是否有一种打印数据类型的方法,如:

Dict()<List()<Dict()>>

Example Data-Structure with Values:

示例数据结构与值:

complexDataStructure = {"FirstDict":[{"AnotherDict":[[1,2,3],[1,2,3] ]}  , {"OneMoreDict":[[1,2,3],[1,2,3] ]}  ]}

>>> output Dict()<List()<Dict()>>

You can see the nested Structure with its Values, I would like to print the Data-types in a similar way and thought about an recursive approach, but not every data-structures are iterable (like set() ) or not all values can be accessed with slicing (dict())

你可以看到嵌套的结构及其值,我想以类似的方式打印数据类型并考虑递归方法,但不是每个数据结构都是可迭代的(比如set())或者不是所有的值都可以通过切片访问(dict())

1 个解决方案

#1


1  

Please see if the following fits your requirements:

请查看以下内容是否符合您的要求:

def ppt(data):
    if isinstance(data, int):
        return "Int()"
    elif isinstance(data, str):
        return "Str()"
    elif isinstance(data, list):
        if len(data) == 0:
            return "List()"
        else:
            return "List()<" + ", ".join([ppt(item) for item in data]) + ">"
    elif isinstance(data, dict):
        if len(data) == 0:
            return "Dict()"
        else:
            return "Dict()<" + ", ".join([ppt(data[key]) for key in data]) + ">"
    else:
        return "Unknown()"

When the input is [{}, []] the output is indeed List()<Dict(), List()>:

当输入是[{},[]]时,输出确实是List() (),list()>

>>> print(ppt([{}, []]))
List()<Dict(), List()>

But when the input is your complexDataStructure the output is:

但是当输入是你的complexDataStructure时,输出是:

Dict()<List()<Dict()<List()<List()<Int(), Int(), Int()>, List()<Int(), Int(), Int()>>>, Dict()<List()<List()<Int(), Int(), Int()>, List()<Int(), Int(), Int()>>>>>

which is different from your Dict()<List()<Dict()>>, but in my opinion is more accurate. Please give me feedback if you have something else in mind.

这与你的Dict() >不同,但在我看来更准确。如果您有其他想法,请给我反馈。 ()

#1


1  

Please see if the following fits your requirements:

请查看以下内容是否符合您的要求:

def ppt(data):
    if isinstance(data, int):
        return "Int()"
    elif isinstance(data, str):
        return "Str()"
    elif isinstance(data, list):
        if len(data) == 0:
            return "List()"
        else:
            return "List()<" + ", ".join([ppt(item) for item in data]) + ">"
    elif isinstance(data, dict):
        if len(data) == 0:
            return "Dict()"
        else:
            return "Dict()<" + ", ".join([ppt(data[key]) for key in data]) + ">"
    else:
        return "Unknown()"

When the input is [{}, []] the output is indeed List()<Dict(), List()>:

当输入是[{},[]]时,输出确实是List() (),list()>

>>> print(ppt([{}, []]))
List()<Dict(), List()>

But when the input is your complexDataStructure the output is:

但是当输入是你的complexDataStructure时,输出是:

Dict()<List()<Dict()<List()<List()<Int(), Int(), Int()>, List()<Int(), Int(), Int()>>>, Dict()<List()<List()<Int(), Int(), Int()>, List()<Int(), Int(), Int()>>>>>

which is different from your Dict()<List()<Dict()>>, but in my opinion is more accurate. Please give me feedback if you have something else in mind.

这与你的Dict() >不同,但在我看来更准确。如果您有其他想法,请给我反馈。 ()