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 | 必需。任何類型(字符串、數字、列表、元組等)。要搜索的值。 |