简体   繁体   English

如何反转python dict中的键顺序?

[英]How to reverse order of keys in python dict?

This is my code :这是我的代码:

a = {0:'000000',1:'11111',3:'333333',4:'444444'}

for i in a:
    print i

it shows:表明:

0
1
3
4

but I want it to show:但我希望它显示:

4
3
1
0

so, what can I do?那么,我能做什么?

The order keys are iterated in is arbitrary.迭代的顺序键是任意的。 It was only a coincidence that they were in sorted order.他们按顺序排列只是巧合。

>>> a = {0:'000000',1:'11111',3:'333333',4:'444444'}
>>> a.keys()
[0, 1, 3, 4]
>>> sorted(a.keys())
[0, 1, 3, 4]
>>> reversed(sorted(a.keys()))
<listreverseiterator object at 0x02B0DB70>
>>> list(reversed(sorted(a.keys())))
[4, 3, 1, 0]

Since Python 3.7 dicts preserve order , which means you can do this now:由于Python 3.7 dicts保留 order ,这意味着您现在可以执行此操作:

my_dict = {'a': 1, 'c': 3, 'b': 2}

for k in reversed(list(my_dict.keys())):
    print(k)

Output:输出:

b
c
a

Since Python 3.8 the built-in reversed() accepts dicts as well, thus you can use:由于Python 3.8内置的reversed()接受字典,因此您可以使用:

for k in reversed(my_dict):
    print(k)

Dictionaries are unordered so you cannot reverse them.字典是无序的,因此您无法反转它们。 The order of the current output is arbitrary.电流输出的顺序是任意的。

That said, you can order the keys of course:也就是说,您当然可以订购钥匙:

for i in sorted(a.keys(), reverse=True):
    print a[i];

but this gives you the reverse order of the sorted keys, not necessarily the reverse order of the keys how they have been added.但这为您提供了排序键的相反顺序,不一定是键添加方式的相反顺序。 Ie it won't give you 1 0 3 if your dictionary was:即,如果您的字典是:它不会给您1 0 3

a = {3:'3', 0:'0', 1:'1'}

Try:尝试:

for i in sorted(a.keys(), reverse=True):
    print i

Python dictionaries don't have any 'order' associated with them. Python 词典没有任何关联的“顺序”。 It's merely a 'coincidence' that the dict is printing the same order. dict 打印相同的顺序只是一个“巧合”。 There are no guarantees that items in a dictionary with come out in any order.不能保证字典中的项目以任何顺序出现。

If you want to deal with ordering you'll need to convert the dictionary to a list.如果要处理排序,则需要将字典转换为列表。

a = list(a) # keys in list
a = a.keys() # keys in list
a = a.values() # values in list
a = a.items() # tuples of (key,value) in list

Now you can sort the list as normal, eg, a.sort() and reverse it as well, eg, a.reverse()现在您可以正常对列表进行排序,例如a.sort()并将其反转,例如a.reverse()

Python dict is not ordered in 2.x. Python dict 在 2.x 中没有排序。 But there's an ordered dict implementation in 3.1 .但是在3.1 中有一个有序的 dict 实现。

for i in reversed(sorted(a.keys())):
    print i

In Python 3.6, which I am using, I reversed the order of keys with their respective values with the help of function update.在我使用的 Python 3.6 中,我在函数更新的帮助下颠倒了键及其各自值的顺序。

original_dict={'A':0,'C':2,'B':1}
new_dict={}
for k,v in original_dict.items():
    dict_element={k:v}
    dict_element.update(new_dict)
    new_dict=dict_element

print(new_dict)

It should print out:它应该打印出来:

{'B':1,'C':2,'A':0}

My 2 ¢.我的 2 美分。

If you want to preserve the insertion order and not the alphabetical ordering, then you can use:如果要保留插入顺序而不是字母顺序,则可以使用:

dict(list(your_dict.keys())[::-1])

Or for the whole dictionary:或者对于整个字典:

dict(list(your_dict.items())[::-1])

If you have a dictionary like this如果你有一本这样的字典

{'faisal2': 2, 'umair': 2, 'fais': 1, 'umair2': 1, 'trending': 2, 'apple': 2, 'orange': 2}

and you want to reverse sort dictionary you can use:并且您想要反向排序字典,您可以使用:

dict(sorted(counts.items(), key=lambda item: item[1],reverse=True))

output will be:输出将是:

{'faisal2': 2, 'umair': 2, 'trending': 2, 'apple': 2, 'orange': 2, 'fais': 1, 'umair2': 1}

just try,你试一试,

INPUT: a = {0:'000000',1:'11111',3:'333333',4:'444444'}输入: a = {0:'000000',1:'11111',3:'333333',4:'444444'}

[x for x in sorted(a.keys(), reverse=True)] [x for x in sorted(a.keys(), reverse=True)]

OUTPUT: [4, 3, 1, 0]输出: [4, 3, 1, 0]

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

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