Python | Count the occurrence of all list items in a tuple



Examples :

  Input:  tuple = (`a`,` a`, `c`,` b`, `d` ) list = [`a`,` b`]  Output:  3  Input:  tuple = (1, 2, 3, 1, 4, 6, 7, 1, 4) list = [1, 4, 7]  Output:  6 

Approach # 1: Naive Approach

First Approach — naive approach. Use a for loop and traverse the given list and count the occurrences of each element of the tuple in the list. Finally, return the score.

# Python3 Program for counting cases
# of all list items in tuple

from collections import Counter

 

def countOccurrence (tup, lst):

count = 0

for item in tup :

if item in lst:

  count + = 1

 

return count 

 
Driver code

tup = ( ` a` , `a` , `c` , ` b` , `d` )

lst = [ `a`  , `b` ]

print (countOccurrence (tup, lst))

Exit :

 3 

Approach # 2 Using a counter
From in the Python Collections module, import the counter to solve this problem. Counter — it is a container that keeps track of how many times equivalent values ​​are added. After storing the result in & # 39; counts & # 39;, we use a for loop and count how many times each item in the list occurs in & # 39; counts & # 39; and sum it up to get the final result.

# Python3 Case Counting Program
# of all list items in a tuple

from collections import Counter

 

def countOccurrence (tup, lst):

  counts = Counter (tup)

return sum (counts [i] for i in lst)

 
Driver code

tup = ( `a` , ` a ` , ` c` , `b` , ` d` )

lst = [ `a` , ` b` ]

print (countOccurrence (tup, lst))

Exit:

 3 

Approach # 3. Using Set
Another method to solve this problem — using the given data structure. Just convert the given list into a set that removes all duplicates. Now, for each element of the list, count its occurrence in a tuple and sum them.

# Python3 Counting program
# of all list items in a tuple

 

def countOccurrence (tup, lst):

lst = set (lst)

return sum ( 1 for x in tup if x in lst)

  
Driver code

tup = ( `a` , `a` , ` c` , `b` , ` d` )

lst = [ `a` , ` b` ]

print (countOccurrence (tup, lst))

Exit :

 3 

Approach # 4 Used Python dictionary definition
Get each tuple element and its frequency as a key: value pair in the Python dictionary, then using a for loop, for each list element, count its occurrences in the tuple and sum them.

# Python3 Case Counting Program
# of all list items in a tuple

 

def countOccurrence (tup, lst):

dct = {}

for i in tup:

  if not dct.get (i):

dct [i] = 0

dct [i] + = 1

  return sum (dct.get (i, 0 ) for i in lst)

 
Driver code

tup = ( `a` , ` a`  , `c` , ` b` , `d` )

lst = [ `a` , ` b` ]

print (countOccurrence (tup, lst))

Exit :

 3 

Approach # 5 : Python numpy.in1d ​​()
Python numpy gives us a direct method to find a solution to a given problem, and this is numpy.in1d ​​() . This method checks if each element of the one-dimensional array is present in the second array. Since the list is also a one-dimensional array, this method can be applied here.

# Python3 Case Counting Program
# of all list items in a tuple

import numpy as np

 

def countOccurrence (tup , lst):

return np .in1d ​​(tup, lst). sum ()

 
Driver code

tup = ( `a` , < / code> `a` , ` c` , `b` , ` d`

lst = [ `a` , ` b` ]

print (countOccurrence (tup, lst))

Exit:

 3