新聞中心
本期又給大家?guī)砹藀ython的函數(shù),與之前不一樣的是,這個(gè)是可以計(jì)算的哦~想要了解的小伙伴一起來看下吧~

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


咨詢
建站咨詢
