Python列表count()方法

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

上一篇: Python列表 下一篇: Python元组