📌  相关文章
📜  Python - 按值列表中的最大/最小元素对字典进行排序

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

Python - 按值列表中的最大/最小元素对字典进行排序

给定字典,根据字典值列表中存在的最大或最小元素执行排序。

方法 #1:使用 sorted() + max() + 字典理解 + reverse + lambda

以上功能的组合可以解决这个问题。在此,我们使用 sorted() 执行排序任务,使用 max() 提取最大值,使用 reverse 获取最大元素第 1 个,并且使用 lambda函数将 max() 逻辑扩展到 sorted 函数。

Python3
# Python3 code to demonstrate working of 
# Sort dictionary by max / min element in value list
# Using sorted() + max() + dictionary comprehension + reverse + lambda
  
# initializing dictionary
test_dict = {"Gfg" : [6, 4], "is" : [10, 3], "best" : [8, 4],
             "for" : [7, 13], "geeks" : [15, 5]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# lambda function driving maximum operation on sorted()
# dictionary comprehension used to reconstruct dictionary
res = {key: test_dict[key] for key in sorted(test_dict, key = lambda ele: max(test_dict[ele]),
       reverse = True)}
  
# printing result 
print("Reverse Sorted dictionary on basis of max values : " + str(res))


Python3
# Python3 code to demonstrate working of 
# Sort dictionary by max / min element in value list
# Using sorted() + min() + dictionary comprehension + reverse + lambda
  
# initializing dictionary
test_dict = {"Gfg" : [6, 4], "is" : [10, 3], "best" : [8, 4],
             "for" : [7, 13], "geeks" : [15, 5]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# lambda function driving minimum operation on sorted()
# dictionary comprehension used to reconstruct dictionary
res = {key: test_dict[key] for key in sorted(test_dict, key = lambda ele: min(test_dict[ele]),
       reverse = True)}
  
# printing result 
print("Reverse Sorted dictionary on basis of min values : " + str(res))


输出
The original dictionary is : {'Gfg': [6, 4], 'is': [10, 3], 'best': [8, 4], 'for': [7, 13], 'geeks': [15, 5]}
Reverse Sorted dictionary on basis of max values : {'geeks': [15, 5], 'for': [7, 13], 'is': [10, 3], 'best': [8, 4], 'Gfg': [6, 4]}

方法 #2:使用 sorted() + min() + 字典理解 + reverse + lambda

这与上述函数的方法类似,唯一的区别是我们根据字典值中的最高最小值进行排序。

蟒蛇3

# Python3 code to demonstrate working of 
# Sort dictionary by max / min element in value list
# Using sorted() + min() + dictionary comprehension + reverse + lambda
  
# initializing dictionary
test_dict = {"Gfg" : [6, 4], "is" : [10, 3], "best" : [8, 4],
             "for" : [7, 13], "geeks" : [15, 5]}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# lambda function driving minimum operation on sorted()
# dictionary comprehension used to reconstruct dictionary
res = {key: test_dict[key] for key in sorted(test_dict, key = lambda ele: min(test_dict[ele]),
       reverse = True)}
  
# printing result 
print("Reverse Sorted dictionary on basis of min values : " + str(res)) 
输出
The original dictionary is : {'Gfg': [6, 4], 'is': [10, 3], 'best': [8, 4], 'for': [7, 13], 'geeks': [15, 5]}
Reverse Sorted dictionary on basis of min values : {'for': [7, 13], 'geeks': [15, 5], 'Gfg': [6, 4], 'best': [8, 4], 'is': [10, 3]}