如何将嵌套的OrderedDict转换为dict?

发布于 2021-01-29 16:56:26

OrderedDict想将其转换为嵌套dictdict()显然,仅在其上应用将转换最后一个条目的最外层。

from collections import OrderedDict

od = OrderedDict(
    [
        (u'name', u'Alice'),
        (u'ID', OrderedDict(
            [
                (u'type', u'card'),
                (u'nr', u'123')
            ]
        )),
        (u'name', u'Bob'),
        (u'ID', OrderedDict(
            [
                (u'type', u'passport'),
                (u'nr', u'567')
            ]
        ))
    ]
)

print(dict(od))

输出:

{u'name': u'Bob', u'ID': OrderedDict([(u'type', u'passport'), (u'nr', u'567')])}

是否有直接方法可以转换所有事件?

关注者
0
被浏览
49
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    最简单的解决方案是使用json转储和加载

    from json import loads, dumps
    from collections import OrderedDict
    
    def to_dict(input_ordered_dict):
        return loads(dumps(input_ordered_dict))
    

    注意:上面的代码适用于被json称为可序列化对象的字典。可以在此处找到默认对象类型的列表

    因此,如果有序词典不包含特殊值,这应该就足够了。

    编辑:基于注释,让我们改进上面的代码。让我们说,input_ordered_dict可能包含默认情况下无法通过json序列化的自定义类对象。在这种情况下,我们应该将default参数json.dumps与我们的自定义序列化程序一起使用。

    (例如):

    from collections import OrderedDict as odict
    from json import loads, dumps
    
    class Name(object):
        def __init__(self, name):
            name = name.split(" ", 1)
            self.first_name = name[0]
            self.last_name = name[-1]
    
    a = odict()
    a["thiru"] = Name("Mr Thiru")
    a["wife"] = Name("Mrs Thiru")
    a["type"] = "test" # This is by default serializable
    
    def custom_serializer(obj):
        if isinstance(obj, Name):
            return obj.__dict__
    
    b = dumps(a) 
    # Produces TypeError, as the Name objects are not serializable
    b = dumps(a, default=custom_serializer)
    # Produces desired output
    

    此示例可以进一步扩展到更大的范围。我们甚至可以根据需要添加过滤器或修改值。只需在custom_serializer函数中添加其他部分

    def custom_serializer(obj):
        if isinstance(obj, Name):
            return obj.__dict__
        else:
            # Will get into this if the value is not serializable by default 
            # and is not a Name class object
            return None
    

    如果是自定义序列化程序,则顶部给出的功能应为:

    from json import loads, dumps
    from collections import OrderedDict
    
    def custom_serializer(obj):
        if isinstance(obj, Name):
            return obj.__dict__
        else:
            # Will get into this if the value is not serializable by default 
            # and is also not a Name class object
            return None
    
    def to_dict(input_ordered_dict):
        return loads(dumps(input_ordered_dict, default=custom_serializer))
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看