📌  相关文章
📜  Python|对字典进行排序

📅  最后修改于: 2022-05-13 01:54:38.300000             🧑  作者: Mango

Python|对字典进行排序

Python,给定一个字典,根据键或值执行排序。 [适用Python >=3.6v]。

案例1:按键排序

此任务使用 sorted() 执行,在此,我们使用 items() 提取的字典项的第一个索引提取键,并将其作为自定义 lambda函数传入 key 以按键排序。添加“reverse=True”以执行反向排序。

Python3
# Python3 code to demonstrate working of 
# Sort a Dictionary
# Sort by Keys
  
# initializing dictionary
test_dict = {"Gfg" : 5, "is" : 7, "Best" : 2, "for" : 9, "geeks" : 8}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# using items() to get all items 
# lambda function is passed in key to perform sort by key 
res = {key: val for key, val in sorted(test_dict.items(), key = lambda ele: ele[0])}
  
# printing result 
print("Result dictionary sorted by keys : " + str(res)) 
  
# using items() to get all items 
# lambda function is passed in key to perform sort by key 
# adding "reversed = True" for reversed order
res = {key: val for key, val in sorted(test_dict.items(), key = lambda ele: ele[0], reverse = True)}
  
# printing result 
print("Result dictionary sorted by keys ( in reversed order ) : " + str(res))


Python3
# Python3 code to demonstrate working of 
# Sort a Dictionary
# Sort by Values 
  
# initializing dictionary
test_dict = {"Gfg" : 5, "is" : 7, "Best" : 2, "for" : 9, "geeks" : 8}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# using items() to get all items 
# lambda function is passed in key to perform sort by key 
# passing 2nd element of items()
res = {key: val for key, val in sorted(test_dict.items(), key = lambda ele: ele[1])}
  
# printing result 
print("Result dictionary sorted by values : " + str(res)) 
  
# using items() to get all items 
# lambda function is passed in key to perform sort by key 
# passing 2nd element of items()
# adding "reversed = True" for reversed order
res = {key: val for key, val in sorted(test_dict.items(), key = lambda ele: ele[1], reverse = True)}
  
# printing result 
print("Result dictionary sorted by values ( in reversed order ) : " + str(res))


输出
The original dictionary is : {'Gfg': 5, 'is': 7, 'Best': 2, 'for': 9, 'geeks': 8}
Result dictionary sorted by keys : {'Best': 2, 'Gfg': 5, 'for': 9, 'geeks': 8, 'is': 7}
Result dictionary sorted by keys ( in reversed order ) : {'is': 7, 'geeks': 8, 'for': 9, 'Gfg': 5, 'Best': 2}

案例 2:按值排序

此任务可以以与上述类似的方式执行,唯一的区别是提取值,items() 的第二个元素作为比较器传递。

Python3

# Python3 code to demonstrate working of 
# Sort a Dictionary
# Sort by Values 
  
# initializing dictionary
test_dict = {"Gfg" : 5, "is" : 7, "Best" : 2, "for" : 9, "geeks" : 8}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# using items() to get all items 
# lambda function is passed in key to perform sort by key 
# passing 2nd element of items()
res = {key: val for key, val in sorted(test_dict.items(), key = lambda ele: ele[1])}
  
# printing result 
print("Result dictionary sorted by values : " + str(res)) 
  
# using items() to get all items 
# lambda function is passed in key to perform sort by key 
# passing 2nd element of items()
# adding "reversed = True" for reversed order
res = {key: val for key, val in sorted(test_dict.items(), key = lambda ele: ele[1], reverse = True)}
  
# printing result 
print("Result dictionary sorted by values ( in reversed order ) : " + str(res))
输出
The original dictionary is : {'Gfg': 5, 'is': 7, 'Best': 2, 'for': 9, 'geeks': 8}
Result dictionary sorted by values : {'Best': 2, 'Gfg': 5, 'is': 7, 'geeks': 8, 'for': 9}
Result dictionary sorted by values ( in reversed order ) : {'for': 9, 'geeks': 8, 'is': 7, 'Gfg': 5, 'Best': 2}