如果我有一个Python字典,我如何获得包含最小值的条目的键?
我在考虑与这个min()功能有关...
鉴于输入:
{320:1, 321:0, 322:3}
Run Code Online (Sandbox Code Playgroud)
它会回来321.
我正在尝试在字典imdb_dict中找到最正和最负的值:
imdb_dict = {'the': '0.0490972013402',
'and': '0.201363575849',
'a': '0.0333946807184',
'of': '0.099837669572',
'to': '-0.0790210365788',
'is': '0.188660139871',
'it': '0.00712569582356',
'in': '0.109215821589',
'i': '-0.154986397986'}
Run Code Online (Sandbox Code Playgroud)
使用此处找到的代码: 获取字典中具有最大值的键?
我可以使用以下方法获得字典的最大值:
imdb_dict = {'the': '0.0490972013402',
'and': '0.201363575849',
'a': '0.0333946807184',
'of': '0.099837669572',
'to': '-0.0790210365788',
'is': '0.188660139871',
'it': '0.00712569582356',
'in': '0.109215821589',
'i': '-0.154986397986'}
Run Code Online (Sandbox Code Playgroud)
但是,我找不到在字典中获得最大负值的方法,因为使用min会显示最接近0的值。是否可以通过修改运算符来获得最大负值的方法,还是有一种更简单的方法来完成该操作?