Python - 字典值列表中第 K 个索引处具有最大元素的键
给定一个值作为列表的字典,任务是编写一个Python程序,通过比较每个列表的元素来获取第 K个索引处具有最大元素的键。
Input : test_dict = {‘Gfg’ : [4, 6, 8, 2],
‘is’ : [1, 4, 5, 9],
‘best’ :[2, 3, 4, 10],
‘for’ :[4, 5, 2, 1],
‘geeks’ :[2, 10, 1, 8]}, K = 3
Output : best
Explanation : 2, 9, 10, 1, 8 are elements and 10 is maximum.
Input : test_dict = {‘Gfg’ : [4, 6, 8, 2],
‘is’ : [1, 4, 5, 9],
‘best’ :[2, 3, 4, 10],
‘for’ :[4, 5, 2, 1],
‘geeks’ :[2, 10, 1, 8]}, K = 2
Output : Gfg
Explanation : 8, 5, 4, 2, 1 are elements and 8 is maximum.
方法一: 使用sorted(), 字典理解和lambda
在此,我们对索引 K 值处的所有列表元素及其 Key 进行反向排序,然后输出提取的最大值的键。
例子:
Python3
# Python3 code to demonstrate working of
# Key with Maximum element at Kth index
# in Dictionary Value List Using sorted()
# + dictionary comprehension + lambda
# initializing dictionary
test_dict = {'Gfg': [4, 6],
'is': [1, 4, 5, 9, 4, 5, 7],
'best': [2, 3, 4, 10],
'for': [4],
'geeks': [2, 10, 1, 10]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing K
K = 3
# sorted sorting all the values in reverse order
# Maximum element is found at 1st position
temp = sorted({key: val[K] if K <= len(val) else -1 for key,
val in test_dict.items()}.items(),
key=lambda sub: sub[1], reverse=True)
# getting all maximum keys in case of multiple
res = []
for idx, ele in enumerate(temp):
res.append(temp[idx][0])
if temp[idx][1] != temp[idx + 1][1]:
break
# printing result
print("The extracted key : " + str(res))
Python3
# Python3 code to demonstrate working of
# Key with Maximum element at Kth index
# in Dictionary Value List Using max()
# + generator expression
# initializing dictionary
test_dict = {'Gfg': [4, 6, 8, 2, 5, 6],
'is': [1],
'best': [2, 3, 4, 10],
'for': [4, 5, 2, 1],
'geeks': [2, 10, 1, 10]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing K
K = 3
# sorted sorting all the values in reverse order
# Maximum element is found at 1st position
# getting maximum
temp = max(test_dict[key][K] if K < len(
test_dict[key]) else -1 for key in test_dict)
# getting all keys with maximum.
res = []
for key in test_dict:
if K < len(test_dict[key]) and test_dict[key][K] == temp:
res.append(key)
# printing result
print("The extracted key : " + str(res))
输出:
The original dictionary is : {‘Gfg’: [4, 6, 8, 2, 5, 6], ‘is’: [1], ‘best’: [2, 3, 4, 10], ‘for’: [4, 5, 2, 1], ‘geeks’: [2, 10, 1, 10]}
The extracted key : [‘best’, ‘geeks’]
方法 2:使用max()和生成器表达式
在这里,我们使用 max() 执行获取最大值的任务,并且生成器表达式用于迭代字典中的所有键。下一步,我们得到与 K 处最大元素匹配的所有键。
例子:
蟒蛇3
# Python3 code to demonstrate working of
# Key with Maximum element at Kth index
# in Dictionary Value List Using max()
# + generator expression
# initializing dictionary
test_dict = {'Gfg': [4, 6, 8, 2, 5, 6],
'is': [1],
'best': [2, 3, 4, 10],
'for': [4, 5, 2, 1],
'geeks': [2, 10, 1, 10]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing K
K = 3
# sorted sorting all the values in reverse order
# Maximum element is found at 1st position
# getting maximum
temp = max(test_dict[key][K] if K < len(
test_dict[key]) else -1 for key in test_dict)
# getting all keys with maximum.
res = []
for key in test_dict:
if K < len(test_dict[key]) and test_dict[key][K] == temp:
res.append(key)
# printing result
print("The extracted key : " + str(res))
输出:
The original dictionary is : {‘Gfg’: [4, 6, 8, 2, 5, 6], ‘is’: [1], ‘best’: [2, 3, 4, 10], ‘for’: [4, 5, 2, 1], ‘geeks’: [2, 10, 1, 10]}
The extracted key : [‘best’, ‘geeks’]