新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythoncount()
python 中的count()函數(shù)有助于返回元組中給定元素的出現(xiàn)次數(shù)。元組是一種內(nèi)置的數(shù)據(jù)類型,用于存儲多個元素,它是有序且不可更改的。

**tuple.count(element)** #where element may be a integer,string,etc.
計數(shù)()參數(shù):
count()函數(shù)接受一個參數(shù)。如果參數(shù)丟失,它將返回一個錯誤。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 元素 | 要計數(shù)的元素 | 需要 |
計數(shù)()返回值
返回值始終是一個整數(shù),它指示特定元素在元組中出現(xiàn)的次數(shù)。如果元組中沒有給定的元素,它將返回零。
| 投入 | 返回值 | | 元素 | 整數(shù)(計數(shù)) |
Python 中count()方法的示例
示例 1:元組計數(shù)()在皮托中是如何工作的?
# vowels tuple
alphabets = ('a', 'b', 'c', 'd', 'a', 'b')
# count element 'a'
count = alphabets .count('a')
# print count
print('The count of a is:', count)
# count element 'c'
count = alphabets .count('c')
# print count
print('The count of c is:', count)
輸出:
The count of a is: 2
The count of c is: 1
示例 2:如何統(tǒng)計 tuple 中的列表和 Tuple 元素?
# random tuple
random_tup = ('a', ('b', 'c'), ('b', 'c'), [1, 2])
# count element ('b', 'c')
count = random_tup.count(('b', 'c'))
# print count
print("The count of ('b', 'c') is:", count)
# count element [1, 2]
count = random_tup.count([1, 2])
# print count
print("The count of [1, 2] is:", count)
輸出:
The count of ('b', 'c') is: 2
The count of [1, 2] is: 1 文章名稱:創(chuàng)新互聯(lián)Python教程:Pythoncount()
URL分享:http://www.dlmjj.cn/article/cdghcsc.html


咨詢
建站咨詢
