在 Python 中对字典进行降序排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35624064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:41:39  来源:igfitidea点击:

Sorting dictionary descending in Python

pythonsortingdictionary

提问by Darien

I am new to Python and I was wondering if there is a more common way of sorting a dictionary in descending order than the way I'm doing it:

我是 Python 的新手,我想知道是否有比我这样做的方式更常见的按降序对字典进行排序的方法:

sorted_dictionary = sorted(dict.items(), key=lambda x: -x[1])

采纳答案by Martijn Pieters

There is a reverseoption to sorted()you could use instead:

您可以使用一个reverse选项sorted()

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

This produces the exact same output, and even works if the values are not numeric.

这会产生完全相同的输出,即使值不是数字也能工作。

回答by Francesco Nazzaro

Python dictionary aren't sortable. Your sorted_dictionaryoutput is not a dictionary but a list. You have to use OrderedDict

Python 字典不可排序。您的sorted_dictionary输出不是字典而是列表。你必须使用OrderedDict

from collections import OrderedDict

sorted_dictionary = OrderedDict(sorted(dict.items(), key=lambda v: v, reverse=True))

回答by Garima Chhikara

Sort in decreasing order of values in python list:

在python列表中按值的降序排序:

items = {'a':6 , 'b':3 , 'c':1 , 'd':9}
sortedItems = sorted(items , key=items.get , reverse = True)
for element in sortedItems :
       print (element, items.get(element))

Output is:

输出是:

d 9
a 6
b 3
c 1

回答by Fuji Komalan

Sorting Dictionary based on values in ascending order

根据值按升序排序字典

d = {'a':5 , 'b':3 , 'd':1 , 'e':2 , 'f':4}
result = sorted(d.items() , key=lambda t : t[1])

for k,v in result:
  print(k,v)

RESULT

结果

d 1
e 2
b 3
f 4
a 5


# d.items=[('a', 5), ('b', 3), ('d', 1), ('e', 2), ('f', 4)]
# lambda t = ('a',5)
# lambda t : t[1] = 5
# result = [('d', 1), ('e', 2), ('b', 3), ('f', 4), ('a', 5)]
# k ,v = ('d',1)
# k = 'd'  
# v = 1

Sorting Dictionary based on values in descending order

根据值按降序排序字典

d = {'a':5 , 'b':3 , 'd':1 , 'e':2 , 'f':4}
result = sorted(d.items() , key=lambda t : t[1] , reverse=True)

for k,v in result:
  print(k,v)

RESULT

结果

 a 5
 f 4
 b 3
 e 2
 d 1


# d.items=[('a', 5), ('b', 3), ('d', 1), ('e', 2), ('f', 4)]
# lambda t = ('a',5)
# lambda t : t[1] = 5
# result = [('a', 5), ('f', 4), ('b', 3), ('e', 2), ('d', 1)]
# k ,v = ('a',5)
# k = 'a'  
# v = 5

回答by Michael Fitzgerald

Here is code that get all the prime factors with exponents for all numbers in a range

这是获取范围内所有数字的所有质因数和指数的代码

    from sympy import primefactors

for num in range (1,10001): k = num prime_factors = {}

对于范围内的 num (1,10001):k = num prime_factors = {}

for i in primefactors(num):
    count = 0
    while num % i == 0:
        num = num/i
        count += 1
    prime_factors[i] = count  

result = sorted(prime_factors.items() , key=lambda t : t[0] , reverse=True)

count = 1
num = k

print '\n%d can be broken down into the following prime factors:' % num,
print '\n%d = ' % num,
for k,v in result:
    if count < len(result):
        print '(%d ** %d) *' % (k,v),
        count += 1
    else:
        print '(%d ** %d)' % (k,v)

回答by thereal90

This should work

这应该工作

{k: v for k, v in sorted(dict.items(), key=lambda item: item[1], reverse = True)}