【发布时间】:2019-10-23 06:46:49
【问题描述】:
我正在尝试获取字典中列表的第二个元素的最小值的键,我在 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
但唯一返回我的,是列表的第一个元素的最小值,而不是我需要的第二个元素
【问题讨论】:
-
如果您会提出负面意见,请发表评论以改进
-
你的minimal reproducible example在哪里?发布不能解决您的问题但没有为您的实际问题提供代码的代码是没有帮助的
-
dict在3.7以下的python版本中没有排序,请注意。 -
您在列表中占据了最小值,而不是列表中的最后一个元素。你需要一个 lambda 函数
min(polo.items(), key=lambda x: x[1][1])[0] -
也许是
min(polo.keys())?
标签: python list dictionary min key-value-store