Python 列表 count() 方法
定义和用法
count() 方法返回具有指定值的元素数量。
实例
例子 1
返回 "cherry" 在 fruits 列表中出现的次数:
fruits = ['apple', 'banana', 'cherry']
x = fruits.count("cherry")
例子 2
返回数值 9 在列表中出现的次数:
points = [1, 4, 2, 9, 7, 8, 9, 3, 1] x = points.count(9)
语法
list.count(value)
参数
| 参数 | 描述 |
|---|---|
| value | 必需。要搜索的值。任何类型(字符串、数字、列表、元组等)。 |