Python列表count()
方法用於返回列表中出現obj
多少次的計數。
語法
以下是count()
方法的語法 -
list.count(obj)
參數
- obj - 這是列表中要計數的對象。
返回值
- 此方法返回列表中出現
obj
多少次的計數。
示例
以下示例顯示了count()
方法的用法。
#!/usr/bin/python3
aList = [123, 'xyz', 'maxsu', 'abc', 123];
print ("Count for 123 : ", aList.count(123))
print ("Count for maxsu : ", aList.count('maxsu'))
當運行上面的程式,它產生以下結果 -
Count for 123 : 2
Count for maxsu : 1