共計(jì) 183 個(gè)字符,預(yù)計(jì)需要花費(fèi) 1 分鐘才能閱讀完成。
可以使用 Python 內(nèi)置函數(shù) count()
來統(tǒng)計(jì)元組中元素的出現(xiàn)次數(shù)。
my_tuple = (1, 2, 3, 4, 2, 1, 3, 2, 1)
element = 2
count = my_tuple.count(element)
print(count)
輸出:
3
上述代碼中,count()
函數(shù)接收一個(gè)參數(shù),即要統(tǒng)計(jì)出現(xiàn)次數(shù)的元素,返回該元素在元組中出現(xiàn)的次數(shù)。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完