Python程序获取由给定键的最大值的索引给出的字典的值
给定一个值作为列表的字典,任务是编写一个Python程序,该程序可以找到任何一个键的最大值并输出其他键值的相似索引列。
方法 :
- 获取给定键的最大值。
- 获取找到的最大元素的索引。
- 检查搜索关键字中是否存在 max 的索引,如果不存在,则返回 Result not possible。
- 获取搜索键 (opt_key) 上的元素,该元素位于步骤 2 中找到的索引处。
Input : test_dict = {“gfg” : [4, 1, 6], “is” : [1, 4, 8], “best” : [9, 10, 1]}, max_search = “best”, opt_key = “gfg”
Output : 1
Explanation : 10 is maximum element in best key, which corresponds to 1st index. In gfg, 1st index is 1, hence 1.
Input : test_dict = {“gfg” : [4, 10, 6], “is” : [1, 4, 8], “best” : [9, 10, 1]}, max_search = “best”, opt_key = “gfg”
Output : 10
Explanation : 10 is maximum element in best key, which corresponds to 1st index. In gfg, 1st index is 10, hence 10.
方法 1:使用index() 、 max( ) 和lambda
在这里,我们使用 max() 找到最大搜索键的最大值,并使用 index() 获取其索引,然后在输出键中获取索引的值。
例子:
Python3
# initializing dictionary
test_dict = {"gfg": [4], "is": [1, 4, 8], "best": [9, 10]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing max_search key
max_search = "best"
# initializing output key
opt_key = "gfg"
# handling case in which maximum index is not
# present in output key
if len(test_dict[opt_key]) <= test_dict[max_search].index(
max(test_dict[max_search])):
res = "Result not possible"
# using max() to get of search key
else:
res = max(test_dict[opt_key], key=lambda sub: test_dict[max_search]
[test_dict[opt_key].index(sub)])
# printing result
print("The required output : " + str(res))
Python3
# initializing dictionary
test_dict = {"gfg": [4, 1, 6], "is": [1, 4, 8], "best": [9, 10, 1]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing max_search key
max_search = "best"
# initializing output key
opt_key = "gfg"
# handling case in which maximum index is not present
# in output key
if len(test_dict[opt_key]) <= test_dict[max_search].index(
max(test_dict[max_search])):
res = "Result not possible"
# using max() to get of search key
# zip() used for combining lists
else:
res = max(zip(test_dict[opt_key], test_dict[max_search]),
key=lambda sub: sub[1])[0]
# printing result
print("The required output : " + str(res))
输出:
The original dictionary is : {‘gfg’: [4], ‘is’: [1, 4, 8], ‘best’: [9, 10]}
The required output : Result not possible
方法 2:使用zip()和max()
在此,使用 zip() 组合搜索和输出键的列表,发布最多找到一个列表并返回相应的输出键索引。
例子:
蟒蛇3
# initializing dictionary
test_dict = {"gfg": [4, 1, 6], "is": [1, 4, 8], "best": [9, 10, 1]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing max_search key
max_search = "best"
# initializing output key
opt_key = "gfg"
# handling case in which maximum index is not present
# in output key
if len(test_dict[opt_key]) <= test_dict[max_search].index(
max(test_dict[max_search])):
res = "Result not possible"
# using max() to get of search key
# zip() used for combining lists
else:
res = max(zip(test_dict[opt_key], test_dict[max_search]),
key=lambda sub: sub[1])[0]
# printing result
print("The required output : " + str(res))
输出:
The original dictionary is : {‘gfg’: [4, 1, 6], ‘is’: [1, 4, 8], ‘best’: [9, 10, 1]}
The required output : 1