小编Ama*_*nda的帖子

获取值大于python中某个整数的字典键的计数

我有一本字典。键是单词,值是这些单词出现的次数。

countDict = {'house': 2, 'who': 41, 'joey': 409, 'boy': 2, 'girl':2}
Run Code Online (Sandbox Code Playgroud)

我想找出有多少元素出现的值大于 1、值大于 20 和值大于 50。

我找到了这个代码

a = sum(1 for i in countDict if countDict.values() >= 2)
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误,我猜这意味着字典中的值不能作为整数处理。

builtin.TypeError: unorderable types: dict_values() >= int()
Run Code Online (Sandbox Code Playgroud)

我尝试修改上面的代码使字典值为整数,但这也不起作用。

a = sum(1 for i in countDict if int(countDict.values()) >= 2)

builtins.TypeError: int() argument must be a string or a number, not 'dict_values'
Run Code Online (Sandbox Code Playgroud)

有什么建议?

python dictionary integer

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

dictionary ×1

integer ×1

python ×1