Python | Count occurrence of all elements of list in a tuple - GeeksforGeeks

15 Mar, 2019
Given a tuple and a list as input, write a Python program to count the occurrences of all items of the list in the 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
The first approach is the naive approach. Use a for loop and traverse through the given list and count the occurrence of each item of tuple in a list. Finally, return the count.
# Python3 Program to count occurrence # of all elements of list in a tuplefrom collections import Counter  def countOccurrence(tup, lst):    count = 0    for item in tup:        if item in lst:            count+= 1          return count       # Driver Codetup = ('a', 'a', 'c', 'b', 'd')lst = ['a', 'b']print(countOccurrence(tup, lst))
3

 Approach #2 : Using CounterFrom Python Collections module, import counter to solve the given problem. A Counter is a container that keeps track of how many times equivalent values are added. Having saved the resultant in ‘counts’, we use a for loop and count how many times each item in list occurs in ‘counts’ and sum it to give the final output.
# Python3 Program to count occurrence # of all elements of list in a tuplefrom collections import Counter  def countOccurrence(tup, lst):    counts = Counter(tup)    return sum(counts[i] for i in lst)      # Driver Codetup = ('a', 'a', 'c', 'b', 'd')lst = ['a', 'b']print(countOccurrence(tup, lst))
3

 Approach #3 : Using SetAnother method of solving the given problem is using set data structure. Simply convert the given list into a set, which removes all duplicates. And now, for each item of list, count its occurrence in tuple and sum them.
# Python3 Program to count occurrence # of all elements of list in a tuple  def countOccurrence(tup, lst):    lst = set(lst)    return sum(1 for x in tup if x in lst)      # Driver Codetup = ('a', 'a', 'c', 'b', 'd')lst = ['a', 'b']print(countOccurrence(tup, lst))
3

 Approach #4 : Using Python dictionaryGet each item of tuple and its frequency as key:value pair in Python dictionary, then using a for loop, for each item of list, count its occurrence in tuple and sum them.
# Python3 Program to count occurrence # of all elements of list 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 Codetup = ('a', 'a', 'c', 'b', 'd')lst = ['a', 'b']print(countOccurrence(tup, lst))
3

 Approach #5 : Python numpy.in1d()Python numpy gives us a direct method to find the solution for the given problem, and that is numpy.in1d(). This method test whether each element of a 1-D array is also present in a second array. Since list is also a 1-D array, this method can be applied here.
# Python3 Program to count occurrence # of all elements of list in a tupleimport numpy as np  def countOccurrence(tup, lst):    return np.in1d(tup, lst).sum()      # Driver Codetup = ('a', 'a', 'c', 'b', 'd') lst = ['a', 'b']print(countOccurrence(tup, lst))
3

Python tuple-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
How to print without newline in Python?