countOf() function in python by R4R Team

- countOf() is a python in-build function which count the occurance of item in sequence.
- this function is predefined in operator module.

Syntax-

operator.countOf(seq,item)

-Here seq is sequence, i.e. list,tuple etc
-It return the occurance of the item in the sequnece.

Example-

list is :[1,2,3,1,1,1]
operator.contains(list,2) will return 1
operator.contains(list,3) will return 4
operator.contains(list,8) will return 0


program-

import operator
l=[1,2,2,2,3,1,1,1,1,1,1,'a','a']
print(operator.countOf(l,8))
print(operator.countOf(l,2))
print(operator.countOf(l,1))
print(operator.countOf(l,3))
print(operator.countOf(l,9))
print(operator.countOf(l,10))
print(operator.countOf(l,'a'))


output-

0
3
7
1
0
0
2


-In this program, we import the module 'operator' because countOf() function is defined inside operator module.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!