Change language

Python | Operator.countOf

Operator.countOf() is used to count the number of occurrences of b in a . Counts the number of occurrences of a value.

Syntax: Operator.countOf (freq = a, value = b)
Parameters:
freq: It can be list or any other data type which store value
value: It is value for which we have to count the number of occurrences

Returns: Count of number of occurrences of value.

Example # 1: Use Operator.countOf () to count the number of occurrences of a given value in the list.

# Show Python program
# using the countOf () function
# in the list

  

from operator impo rt *  

arr = [ 1 , 2 , 3 , 3 , 3 ]

arr1 = [ ’a ’ , ’ b’ , ’c’ , ’ b’ , ’d’ ]

  

print ( "Number of occurrence of b in arr1 = " , countOf (arr1, " b " ))

print ( "Number of occurrence of e in arr1 = " , countOf (arr1, " e " ))

print ( "Number of occurrence of 3 in arr = " , countOf (arr, 3 ))

Exit:

 Number of occurrence of b in arr1 = 2 Number of occurrence of e in arr1 = 0 Number of occurrence of 3 in arr = 3 

Example # 2: Use Operator.countOf () to count the number of occurrences of a given value in the dictionary.

# Show Python program
# use countOf function ()
# in the dictionary

 

from operator import *

 

d = { " score " : 3 , "score1" : 1 , "score2" : 3 , "score3" : 1 , "score4" : 3 }

 

print ( "Number of occurrence of three in dict =" , countOf (d.values ​​(), 3 ))

print ( " Number of occurrence of one in dict = " , countOf (d.values ​​(), 1 ))

print ( "Number of occurrence of four in dict =" , countOf (d.values ​​(), 4 ))

Output:

< pre> Number of occurrence of three in dict = 3 Number of occurrence of one in dict = 2 Number of occurrence of four in dict = 0

Example # 3: Use Operator.countOf () to count the number of occurrences of a given value in tuples.

# Show Python program
# using the countOf () function
# in tuples

 

from operator import *

 

tup = ( 1 , 2 , 3 , 3 , 3 )

tup1 = ( ’a’ , ’b’ , ’ c’ , ’b’ , ’ d’ )

 

print ( "Number of occurrence of b in tuple1 =" , countOf (tup1, "b" ))

print ( " Number of occurrence of e in tuple1 = " < code class = "plain">, countOf (tup1, "e" ))

print ( "Number of occurrence of 3 in tuple =" , countOf (tup, 3 ))

Output:

 Number of occurrence of b in tuple1 = 2 Number of occurrence of e in tuple1 = 0 Number of occurrence of 3 in tuple = 3 

Shop

Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$
Gifts for programmers

Best laptop for Zoom

$499
Gifts for programmers

Best laptop for Minecraft

$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically