在这篇文章中,我们将了解count() 用于元组的方法Python.这List count()一种方法元组返回给定元素在中出现的次数元组.
示例
Python3
tuple = (1, 2, 3, 1, 2, 3, 1, 2, 3)
print(tuple.count(3))
输出:
3
Python 元组 count() 方法语法
用法: tuple.count( ele )
参数:
- ele: 我们想要计算其出现次数的任何元素。
返回:元素在元组中出现的次数。
Python 示例中的元组 count() 方法
在Python中计算元组中元素的频率
在这里,我们使用 count 方法计算给定元组中特定元素的总出现次数。
Python3
# Creating tuples
Tuple1 = (0, 1, 2, 3, 2, 3, 1, 3, 2)
Tuple2 = ('python', 'geek', 'python',
'for', 'GFG', 'python', 'geeks')
# count the appearance of 3
res = Tuple1.count(3)
print('Count of 3 in Tuple1 is:', res)
# count the appearance of python
res = Tuple2.count('python')
print('Count of Python in Tuple2 is:', res)
输出:
Count of 3 in Tuple1 is: 3 Count of Python in Tuple2 is: 3
计算元组内列表元素的数量
在这里,我们使用 count 方法计算特定元素的总出现次数,在本例中是 list 和元组内的元组。
Python3
# Creating tuples
Tuple = (0, 1, "GFG", [3,2], 1,
[3, 2], 'geeks', (0), ('G', 'F'))
# count the appearance of [3, 2]
res = Tuple.count([3, 2])
print('Count of [3, 2] in Tuple is:', res)
输出:
Count of [3, 2] in Tuple is: 2
统计元组中不存在的元素
在这里,我们使用 count 方法计算给定元组中实际不存在的特定元素的总出现次数。
Python3
# Creating tuples
Tuple1 = (0, 1, 2, 3, 2, 3, 1, 3, 2)
Tuple2 = ('python', 'geek', 'python',
'for', 'GFG', 'python', 'geeks')
# count the appearance of 3
res = Tuple1.count(5)
print('Count of 5 in Tuple1 is:', res)
# count the appearance of python
res = Tuple2.count('GeeksforGeeks')
print('Count of GeeksforGeeks in Tuple2 is:', res)
输出:
Count of 5 in Tuple1 is: 0 Count of GeeksforGeeks in Tuple2 is: 0
计算元组中元组的出现次数
在此示例中,我们将元组嵌套在元组内,并检查 tuple 在元组内出现的次数。
Python3
my_tuple = ((1, 2), ('a', 'b'), (1, 2), ('c', 'd'), ('a', 'b'))
count_tuple = my_tuple.count((1, 2))
print(count_tuple)
count_xy = my_tuple.count(('x', 'y'))
print(count_xy)
输出:
2 0
相关用法
- Python Tuple count()用法及代码示例
- Python Tuple cmp()用法及代码示例
- Python Tuple index()用法及代码示例
- Python Tuple len()用法及代码示例
- Python Tuple max()用法及代码示例
- Python Tuple min()用法及代码示例
- Python Tuple tuple()用法及代码示例
- Python Tuple Matrix转Tuple List用法及代码示例
- Python Tuples转Dictionary用法及代码示例
- Python Tuple转integer用法及代码示例
- Python Tuple转List用法及代码示例
- Python Tuple转Json Array用法及代码示例
- Python Tuple转Tuple Pair用法及代码示例
- Python Tkinter grid()用法及代码示例
- Python TextCalendar formatmonth()用法及代码示例
- Python TextCalendar formatyear()用法及代码示例
- Python TextCalendar prmonth()用法及代码示例
- Python TextCalendar pryear()用法及代码示例
- Python Thread getName()用法及代码示例
- Python Thread is_alive()用法及代码示例
- Python Thread join()用法及代码示例
- Python Thread run()用法及代码示例
- Python Thread setName()用法及代码示例
- Python Thread start()用法及代码示例
- Python Timer cancel()用法及代码示例
注:本文由纯净天空筛选整理自shlokdi35dq大神的英文原创作品 Python Tuple count() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。