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 | 必需。任何类型(字符串、数字、列表、元组等)。要搜索的值。 |
如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com
如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com