Python3 List count()方法
描述
count() 方法用於統計某個元素在列表中出現的次數。
語法
count()方法語法:
list.count(obj)
參數
- obj -- 列表中統計的對象。
返回值
返回元素在列表中出現的次數。
實例
以下實例展示了 count()函數的使用方法:
實例
#!/usr/bin/python3
aList = [123, 'Google', 'zaixian', 'Taobao', 123];
print ("123 元素個數 : ", aList.count(123))
print ("zaixian 元素個數 : ", aList.count('zaixian'))
aList = [123, 'Google', 'zaixian', 'Taobao', 123];
print ("123 元素個數 : ", aList.count(123))
print ("zaixian 元素個數 : ", aList.count('zaixian'))
以上實例輸出結果如下:
123 元素個數 : 2 zaixian 元素個數 : 1