首页 > 解决方案 > 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)

标签: python

解决方案


您正在描述内置函数的基本用法sum

>>> data = [204, 204, 204, 500, 204, 204, 500, 500, 204, 404] 
>>> sum(1 for n in data if n != 204) 
4

推荐阅读