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