简体   繁体   English

嵌套字典 Python 的倒序

[英]Reverse Order of a Nested Dictionary Python

I have a two-layered dictionary and would like a simple command or function (if possible) to reverse the order of the dictionary, but only the outer layer, not the inner.我有一个两层字典,想要一个简单的命令或函数(如果可能)来反转字典的顺序,但只有外层,而不是内层。 For instance, if my dictionary were as follows:例如,如果我的字典如下:

{'dictA': {'key_1': 'value_1'}, 'dictB': {'key_2': 'value_2'}, 'dictC': {'key_3', 'value_3'}}

and I wanted to instead make it the following:我想改为如下:

{'dictC': {'key_3', 'value_3'}, 'dictB': {'key_2': 'value_2'}, 'dictA': {'key_1': 'value_1'}}

How would I do this?我该怎么做? I have tried methods like invertdict() and inv_map = {v: k for k, v in my_map.items()} , but I had no success with either.我尝试过invertdict()和 inv_map = {v: k for k, v in my_map.items()} ,但我都没有成功。

Any help is appreciated!任何帮助表示赞赏!

Reverse the items:反转项目:

>>> dict(reversed(d.items()))
{'dictC': {'value_3', 'key_3'}, 'dictB': {'key_2': 'value_2'}, 'dictA': {'key_1': 'value_1'}}

以下代码可以解决问题:

dict(reversed(current_dct.items()))

If your using Python 3.6 or lower, dictionary order is not maintained or guaranteed.如果您使用 Python 3.6 或更低版本,则不维护或保证字典顺序。 What you could do is use a collections.OrderedDict() and applyreversed() to reverse the dictionary:您可以做的是使用collections.OrderedDict()并应用reversed()来反转字典:

>>> from collections import OrderedDict
>>> d = {'dictA': {'key_1': 'value_1'}, 'dictB': {'key_2': 'value_2'}, 'dictC': {'key_3', 'value_3'}}
>>> OrderedDict(reversed(list(d.items())))
OrderedDict([('dictC', {'value_3', 'key_3'}), ('dictB', {'key_2': 'value_2'}), ('dictA', {'key_1': 'value_1'})])

OrderedDict() is a subclass of dict , so it works like a normal dictionary. OrderedDict()dict的子类,因此它的工作方式类似于普通字典。

However, if you are using a newer version of Python, then dictionary insertion order is maintained.但是,如果您使用的是较新版本的 Python,则会保留字典插入顺序。 So you could just do the following:因此,您可以执行以下操作:

>>> dict(reversed(list(d.items())))
{'dictC': {'value_3', 'key_3'}, 'dictB': {'key_2': 'value_2'}, 'dictA': {'key_1': 'value_1'}}

Note: For safety reasons, its probably better to cast list() to d.items , since older versions pre Python 3.8 don't allow reversing of type dict_items .注意:出于安全原因,将list()d.items可能更好,因为 Python 3.8 之前的旧版本不允许反转dict_items类型。 You will raise a TypeError: 'dict_items' is not reversible error otherwise.否则,您将引发TypeError: 'dict_items' is not reversible错误。

The solutions that implement reversed don't seem to work in older versions of Python ie pre 3.8.实现reversed的解决方案似乎不适用于旧版本的 Python,即 3.8 之前的版本。 This solution should work in Python 3.x此解决方案应该适用于 Python 3.x

Code代码

so_dict = {'dictA': {'key_1': 'value_1'}, 'dictB': {'key_2': 'value_2'}, 'dictC': {'key_3', 'value_3'}}

{k: v for k, v in sorted(list(so_dict.items()), key=lambda x:x[0].lower(), reverse=True)}

Output输出

{'dictC': {'key_3', 'value_3'},
 'dictB': {'key_2': 'value_2'},
 'dictA': {'key_1': 'value_1'}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM