python元组count()

时间:2020-02-23 14:43:37  来源:igfitidea点击:

在本教程中,我们将看到Python元组Count方法.Python Tuple的Count方法用于计算元组中的元素的出现次数。

Python元组计数语法

tuple1.count(element)

tuple1是元组和元素的对象是我们想要获取计数的对象。

Python元组计数示例

让我们了解例子的计数方法。

t=(1,2,2,3,2,3,2,3,4,2,7,7,5)
print("Count of 2 is:",t.count(2))
tupleOfCounties=('China','Netherlands','China','Bhutan')
print("Count of 'China' is:",tupleOfCounties.count('China'))

输出:

Count of 2 is: 5
Count of 'China' is: 2