新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythoncount()方法怎么用?怎么計數(shù)?
本期又給大家?guī)砹藀ython的函數(shù),與之前不一樣的是,這個是可以計算的哦~想要了解的小伙伴一起來看下吧~

Python列表計數(shù)count()方法
count()是Python中的內(nèi)置函數(shù)。 它將返回列表中給定元素的總數(shù)。 count()函數(shù)用于對列表中的元素以及字符串進行計數(shù)。
語法:
list.count(element)
參數(shù):
element:是我們要查找計數(shù)的元素.
返回值:
count()方法將返回一個整數(shù)值,即給定列表中給定元素的計數(shù)。 如果在給定列表中找不到該值,則返回0.
示例1:列表計數(shù)
以下示例顯示了list()函數(shù)的工作方式:
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:查找給定列表中的元素計數(shù)(重復(fù)項)
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
總結(jié):
count()是Python中的內(nèi)置函數(shù)。 它將返回列表或字符串中給定元素的個數(shù)。
對于列表,需要將計數(shù)的元素傳遞給count()函數(shù),它將返回該元素的個數(shù)。
count()方法返回一個整數(shù)值。
以上就是關(guān)于count()方法的全部內(nèi)容了,如需了解更多python實用知識,點擊進入PyThon學習網(wǎng)教學中心。
標題名稱:創(chuàng)新互聯(lián)Python教程:Pythoncount()方法怎么用?怎么計數(shù)?
網(wǎng)頁網(wǎng)址:http://m.5511xx.com/article/dhpdsgi.html


咨詢
建站咨詢
