Python – 按键的值列表索引对字典列表进行排序
给定字典列表,根据 Key 的索引值对字典进行排序。
Input : [{“Gfg” : [6, 7, 8], “is” : 9, “best” : 10},
{“Gfg” : [2, 0, 3], “is” : 11, “best” : 19},
{“Gfg” : [4, 6, 9], “is” : 16, “best” : 1}], K = “Gfg”, idx = 0
Output : [{‘Gfg’: [2, 0, 3], ‘is’: 11, ‘best’: 19}, {‘Gfg’: [4, 6, 9], ‘is’: 16, ‘best’: 1}, {‘Gfg’: [6, 7, 8], ‘is’: 9, ‘best’: 10}]
Explanation : 2<4<6, hence dictionary ordered in that way by 0th index of Key.
Input : [{“Gfg” : [6, 7, 8], “is” : 9, “best” : 10},
{“Gfg” : [2, 0, 3], “is” : 11, “best” : 19},
{“Gfg” : [4, 6, 9], “is” : 16, “best” : 1}], K = “Gfg”, idx = 1
Output : [{‘Gfg’: [2, 0, 3], ‘is’: 11, ‘best’: 19}, {‘Gfg’: [4, 6, 9], ‘is’: 16, ‘best’: 1}, {‘Gfg’: [6, 7, 8], ‘is’: 9, ‘best’: 10}]
Explanation : 0<6<7, hence dictionary ordered in that way by 1st index.
方法 #1:使用 sorted() + lambda
上述功能的组合可以用来解决这个问题。在此,我们使用 sorted 进行排序,并在 lambda函数中提供了基于列表索引的逻辑。
Python3
# Python3 code to demonstrate working of
# Sort dictionaries list by Key's Value list index
# Using sorted() + lambda
# initializing lists
test_list = [{"Gfg" : [6, 7, 8], "is" : 9, "best" : 10},
{"Gfg" : [2, 0, 3], "is" : 11, "best" : 19},
{"Gfg" : [4, 6, 9], "is" : 16, "best" : 1}]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = "Gfg"
# initializing idx
idx = 2
# using sorted() to perform sort in basis of 1 parameter key and
# index
res = sorted(test_list, key = lambda ele: ele[K][idx])
# printing result
print("The required sort order : " + str(res))
Python3
# Python3 code to demonstrate working of
# Sort dictionaries list by Key's Value list index
# Using sorted() + lambda (Additional parameter in case of tie)
# initializing lists
test_list = [{"Gfg" : [6, 7, 9], "is" : 9, "best" : 10},
{"Gfg" : [2, 0, 3], "is" : 11, "best" : 19},
{"Gfg" : [4, 6, 9], "is" : 16, "best" : 1}]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = "Gfg"
# initializing idx
idx = 2
# initializing K2
K2 = "best"
# using sorted() to perform sort in basis of 2 parameter key
# inner is evaluated after the outer key in lambda order
res = sorted(sorted(test_list, key = lambda ele: ele[K2]), key = lambda ele: ele[K][idx])
# printing result
print("The required sort order : " + str(res))
The original list : [{‘Gfg’: [6, 7, 8], ‘is’: 9, ‘best’: 10}, {‘Gfg’: [2, 0, 3], ‘is’: 11, ‘best’: 19}, {‘Gfg’: [4, 6, 9], ‘is’: 16, ‘best’: 1}]
The required sort order : [{‘Gfg’: [2, 0, 3], ‘is’: 11, ‘best’: 19}, {‘Gfg’: [6, 7, 8], ‘is’: 9, ‘best’: 10}, {‘Gfg’: [4, 6, 9], ‘is’: 16, ‘best’: 1}]
方法 #2 : 使用 sorted() + lambda (附加参数在 tie 的情况下)
这是对值排序的修改,添加另一个参数以防列表中的值并列。
Python3
# Python3 code to demonstrate working of
# Sort dictionaries list by Key's Value list index
# Using sorted() + lambda (Additional parameter in case of tie)
# initializing lists
test_list = [{"Gfg" : [6, 7, 9], "is" : 9, "best" : 10},
{"Gfg" : [2, 0, 3], "is" : 11, "best" : 19},
{"Gfg" : [4, 6, 9], "is" : 16, "best" : 1}]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = "Gfg"
# initializing idx
idx = 2
# initializing K2
K2 = "best"
# using sorted() to perform sort in basis of 2 parameter key
# inner is evaluated after the outer key in lambda order
res = sorted(sorted(test_list, key = lambda ele: ele[K2]), key = lambda ele: ele[K][idx])
# printing result
print("The required sort order : " + str(res))
The original list : [{‘Gfg’: [6, 7, 9], ‘is’: 9, ‘best’: 10}, {‘Gfg’: [2, 0, 3], ‘is’: 11, ‘best’: 19}, {‘Gfg’: [4, 6, 9], ‘is’: 16, ‘best’: 1}]
The required sort order : [{‘Gfg’: [2, 0, 3], ‘is’: 11, ‘best’: 19}, {‘Gfg’: [4, 6, 9], ‘is’: 16, ‘best’: 1}, {‘Gfg’: [6, 7, 9], ‘is’: 9, ‘best’: 10}]