Python 将两个 List 转为字典,取字典值 (value) 的最值,并得到对应的键 (key)

method 1

list1 = [‘1’,‘2’,‘3’]
list2 = [‘a’,‘b’,‘c’]
dict1 = dict(map(lambda x,y:[x,y],list1,list2))

result

print(dict1)
[Out] {‘1’: ‘a’, ‘2’: ‘b’, ‘3’: ‘c’}

method 2

dict(zip(list1,list2))
[Out] {‘1’: ‘a’, ‘2’: ‘b’, ‘3’: ‘c’}

求最小的 value

minValue = min(dict1.values())

通过遍历,通过值求键

for k,v in dict1.items():
    if v == minValue:
        print(k)

猜你喜欢

转载自blog.csdn.net/qq_35762060/article/details/110500372