python - Python - 获取列表中的项目数不等于某事
问题描述
您如何获得此列表中不是 204 的项目数?
data = [204, 204, 204, 500, 204, 204, 500, 500, 204, 404]
number_of_not_204 = any(x != 204 for x in data)
# returns True
print number_of_not_204
# looking to get the number 4 (500, 500, 500, and 404 are not 204)
解决方案
您正在描述内置函数的基本用法sum
:
>>> data = [204, 204, 204, 500, 204, 204, 500, 500, 204, 404]
>>> sum(1 for n in data if n != 204)
4
推荐阅读
- unreal-engine4 - 无法在虚幻引擎 4 中编译 Actor 类 - 错误 MSB3073 以代码 6 退出
- scala - 模式匹配 - 值不是绑定变量的成员
- postgresql - COPY FROM 错误地跳过列
- spyder - Spyder Variable Explorer: Disable Scientific Notation
- java - 使用 IsoChronology 的闰年天数
- django - 使用邮递员发布请求 Manytomany 字段 DRF
- woocommerce - woocommerce 中每个产品的多个参考
- ethereum - 如果我增加循环次数,Solidity Code 给出气体估计失败
- javascript - 从打字稿中的基类调用要在子类中实现的方法
- python - 数组中元素的意外更改