How do I count the number of one element in an array in Python?
Method1: count()
Using the count() function you will get the number of one element in an array in python. It is a built-in function in python.
list = [1,9,5,3,9,2,6,9,9,0,9]
count = list.count(9)
print('The count of element: 9 is ', count)
Output:
The count of element: 9 is 5
Method2:
list = [1,2,6,6,6,8,6,6,6]
count = 0
for i in list:
if(i==6):
count = count + 1
print('The count of element: 6 is ', count)
Output:
The count of element: 6 is 6
I hope it helps you.
Comments
Post a Comment