我正在尝试在字典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的值。是否可以通过修改运算符来获得最大负值的方法,还是有一种更简单的方法来完成该操作?
我怎样才能找到具有最大值的字典键,当它们是一个层时,我们将采用按字母顺序排列的第一个键
a = {'f':3, 't':5, 'c':5}
ma = max(a, key = a.get)
Run Code Online (Sandbox Code Playgroud)
这将返回 t 而不是 c
production = {
'item1': '500',
'item2': '10000',
}
Run Code Online (Sandbox Code Playgroud)
我试图从该 dict 中获得最高值,即 10000。但是,我得到 500 作为max(production.values()).
我相信它是这样的,因为它从 string 中获得最高值,而不是 int (采用其代码点的字典顺序)。
有人可以帮我解决一个问题。谢谢!
我正在做以下工作,我创建了一个字典对象,如下所示,我需要定义 3 个不同的函数。一个用于 maxScore 的函数,一个用于 minScore 的函数,以及一个用于求平均值以及打印所有 3 的函数。
我的字典对象代码如下
scores = {}
scores['Andy'] = 78
scores['Bill'] = 82
scores['Cindy'] = 94
scores['Dave'] = 77
scores['Emily'] = 82
scores['Frank'] = 94
scores['Gene'] = 87
Run Code Online (Sandbox Code Playgroud)
我似乎无法正确定义函数来打印结果。