Python列表计数count()方法
count()是Python中的内置函数。 它将返回列表中给定元素的总数。 count()函数用于对列表中的元素以及字符串进行计数。
在此Python教程中,我们将学习:
语法:

list.count(element)
参数:
element:是我们要查找计数的元素.
返回值:
count()方法将返回一个整数值,即给定列表中给定元素的计数。 如果在给定列表中找不到该值,则返回0.
示例1:列表计数
以下示例显示了list()函数的事情办法:
list1 = ['red', 'green', 'blue', 'orange', 'green', 'gray', 'green']color_count = list1.count('green')print('The count of color: green is ', color_count)
输出:
The count of color: green is 3
示例2:查找给定列表中的元素计数(重复项)
list1 = [2,3,4,3,10,3,5,6,3]elm_count = list1.count(3)print('The count of element: 3 is ', elm_count)
输出:
The count of element: 3 is 4
择要:
count()是Python中的内置函数。 它将返回列表或字符串中给定元素的个数。对付列表,须要将计数的元素通报给count()函数,它将返回该元素的个数。count()方法返回一个整数值。希望这篇文章对你们有用,
欢迎不才方谈论留言,
感激关注.