获取字典中列表的第二个元素

时间:2019-06-07 18:45:37

标签: python list dictionary min key-value-store

我正在尝试获取字典中列表第二个元素的最小值的键,我已经在stackoverflow中进行了搜索,但是我什么也没得到,我找到的最接近的是下一个代码:

import operator
polo={78:[100,1],12:[101,0]}#
print(min(polo.items(), key=operator.itemgetter(1))[0])#i want that the operator compare "1" of the first list and "0" of the second key of the dict and give me the key that have the min value between that values 
#expected output was 12 but give me 78 

但返回my的唯一东西是列表中第一个元素的最小值,而不是我需要它的第二个元素

2 个答案:

答案 0 :(得分:0)

polo={78:[100,1],12:[101,0]}
print(sorted((v[1], k) for k, v in polo.items())[0][-1])

此打印:

12

答案 1 :(得分:0)

polo={78:[100,1],12:[101,0]}

def find(dict):
    secondElements = {}    
    for key, value in dict.items():
        secondElements[value[1]] = key
    return secondElements.get(min(secondElements))

print(find(polo));