Python3 列表 Python3 列表


描述

count() 方法用于統(tǒng)計(jì)某個(gè)元素在列表中出現(xiàn)的次數(shù)。

語(yǔ)法

count()方法語(yǔ)法:

list.count(obj)

參數(shù)

  • obj -- 列表中統(tǒng)計(jì)的對(duì)象。

返回值

返回元素在列表中出現(xiàn)的次數(shù)。

實(shí)例

以下實(shí)例展示了 count()函數(shù)的使用方法:

#!/usr/bin/python3

aList = [123, 'Google', 'W3CSchool', 'Taobao', 123];

print ("123 元素個(gè)數(shù) : ", aList.count(123))
print ("W3CSchool 元素個(gè)數(shù) : ", aList.count('W3CSchool'))

以上實(shí)例輸出結(jié)果如下:

123 元素個(gè)數(shù) :  2
W3CSchool 元素個(gè)數(shù) :  1

Python3 列表 Python3 列表