d = {1: 3, 2: 5, 3: 2, 5: 1, 4: 1}
方法1:转化为列表
k = list(d.keys()) # [1,2,3,5,4]
v = list(d.values()) #[3,5,2,1,1]
k[v.index(5)] #找到value为5的key
方法2:使用生成器generator
k for k,v in d.items() if v == 5 #这是一个generator
ans = [k for k,v in d.items() if v == 5] #ans是一个list,ans = [2]
方法3:key和value互换
nd = {v : k for k,v in d.items()}
#注意此时nd = {3: 1, 5: 2, 2: 3, 1: 4},因为d中4和5的value都为1