Python | Find sum of frequency of given elements in the list
Given two lists containing integers, the task is to find the sum of the frequency of elements of the first list in the second list.
Example:
Input: list1 = [1, 2, 3]
list2 = [2, 1, 2, 1, 3, 5, 2, 3]
Output: 7
Explanation:
No of time 1 occurring in list2 is :2
No of time 2 occurring in list2 is :3
No of time 3 occurring in list2 is :2
Sum = 2+3+2 = 7
Below are some ways to achieve the above tasks.
Method #1: Using sum()
Python3
Input1 = [ 1 , 2 , 3 ]
Input2 = [ 2 , 1 , 2 , 1 , 3 , 5 , 2 , 3 ]
Output = sum (Input2.count(elem) for elem in Input1)
print ( "Initial list are:" , Input1, Input2)
print ( "Frequency is:" , Output)
|
Output:
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using sum() and Counter()
Python3
from collections import Counter
Input1 = [ 1 , 2 , 3 ]
Input2 = [ 2 , 1 , 2 , 1 , 3 , 5 , 2 , 3 ]
temp = Counter(Input2)
Output = sum (temp[x] for x in Input1)
print ( "Initial list are:" , Input1, Input2)
print ( "Frequency is:" , Output)
|
Output:
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7
Method #3: Using the reduce() function from the functools module:
This approach involves using the reduce() function to iterate through the elements in the first list and add up the frequencies of those elements in the second list. Here’s an example of how it could be done:
Python3
from functools import reduce
Input1 = [ 1 , 2 , 3 ]
Input2 = [ 2 , 1 , 2 , 1 , 3 , 5 , 2 , 3 ]
sum_freq = reduce ( lambda x, y: x + Input2.count(y), Input1, 0 )
print ( "Initial list are:" , Input1, Input2)
print ( "Frequency is:" , sum_freq)
|
Output
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7
Method #4: Using operator.countOf() method
Python3
import operator as op
Input1 = [ 1 , 2 , 3 ]
Input2 = [ 2 , 1 , 2 , 1 , 3 , 5 , 2 , 3 ]
Output = sum (op.countOf(Input2,elem) for elem in Input1)
print ( "Initial list are:" , Input1, Input2)
print ( "Frequency is:" , Output)
|
Output
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7
Time Complexity: O(N), where n is the length of the given list
Auxiliary Space: O(N)
Method #5: Using filter() method
In this example, we have initialized the test list1 and test list2. We have used the filter function on list2 which filters out the element that is not in list1. The length of the list is the final answer.
Python3
Input1 = [ 1 , 2 , 3 ]
Input2 = [ 2 , 1 , 2 , 1 , 3 , 5 , 2 , 3 ]
Output = len ([ * filter ( lambda a : a in Input1, Input2)])
print ( "Initial list are:" , Input1, Input2)
print ( "Frequency is:" , Output)
|
Output :
Initial list are: [1, 2, 3] [2, 1, 2, 1, 3, 5, 2, 3]
Frequency is: 7
Time complexity: O(M*N), Where M is the length of list 1 and N is the length of list 2.
Auxiliary space: O(M), Where M is the length of the filtered list.
Last Updated :
24 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...