Count() function of array class

Method Name:

count

Signature:

count(elem)

Return Value:

The number of times an element elem is present in an array object.

Overview:

  • The count() method returns the number of occurrences of an element in the array.

Example:

# Example Python program that counts the number of occurrences of a specific

# element present in an array

import array

 

# Create an array of signed integers

numbers = array.array('i');

 

# Add elements to the array

numbers.append(1);

numbers.append(2);

 

# Add 3 twice

numbers.append(3);

numbers.append(3);

 

# Count the number of occurrences of 3

num2Find = 3;

occurences = numbers.count(num2Find);

print("Number of occurrences of %d:"%num2Find);

print(occurences);

 

Output:

Number of occurrences of 3:

2

 

 


Copyright 2024 © pythontic.com