count() 方法返回 obj 對象出現在列表的數量。
語法
以下是 count() 方法的語法-
list.count(obj)
參數
-
obj -- 這是在列表中要被計數的對象。
返回值
這個方法返回obj對象出現在列表計數。
對象
下麵的示例顯示 count() 方法的使用。
#!/usr/bin/python3 aList = [123, 'xyz', 'zara', 'abc', 123]; print ("Count for 123 : ", aList.count(123)) print ("Count for zara : ", aList.count('zara'))
當我們運行上面的程式,會產生以下結果 -
Count for 123 : 2 Count for zara : 1