Python – Sum of each List element occurrence in another
Sometimes, while working with Python, we can have a problem in which we need to get occurrence of 1 element in another. But as a modification of this, we can have a problem in which we need to count the occurrence of all elements of 1 list in another. Lets discuss certain ways in which this task can be performed.
Method #1 : Using nested loops This is one of the way in which this task can be performed. This is brute force way in which this task can be performed. In this, we iterate one list and then target list, if element match, we increase the counter.
Python3
test_list1 = [ 1 , 3 , 4 , 5 , 1 , 4 , 4 , 6 , 7 ]
test_list2 = [ 4 , 6 , 1 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = 0
for ele in test_list2:
for ele1 in test_list1:
if ele1 = = ele:
res = res + 1
print ( "The occurrence count : " + str (res))
|
Output
The original list 1 is : [1, 3, 4, 5, 1, 4, 4, 6, 7]
The original list 2 is : [4, 6, 1]
The occurrence count : 6
Time Complexity : O(n*m), where n is length of test_list1 and m is length of test_list2
Auxiliary Space : O(n+m), where n is length of test_list1 and m is length of test_list2
Method #2 : Using sum() + count() The combination of above methods can be used to perform this particular task. This is one liner alternative to the above method. In this counting is done using count() and accumulation using sum().
Python3
test_list1 = [ 1 , 3 , 4 , 5 , 1 , 4 , 4 , 6 , 7 ]
test_list2 = [ 4 , 6 , 1 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = sum (test_list1.count(idx) for idx in test_list2)
print ( "The occurrence count : " + str (res))
|
Output
The original list 1 is : [1, 3, 4, 5, 1, 4, 4, 6, 7]
The original list 2 is : [4, 6, 1]
The occurrence count : 6
Method 3: using operator.countOf() method
Python3
import operator as op
test_list1 = [ 1 , 3 , 4 , 5 , 1 , 4 , 4 , 6 , 7 ]
test_list2 = [ 4 , 6 , 1 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = sum (op.countOf(test_list1, idx) for idx in test_list2)
print ( "The occurrence count : " + str (res))
|
Output
The original list 1 is : [1, 3, 4, 5, 1, 4, 4, 6, 7]
The original list 2 is : [4, 6, 1]
The occurrence count : 6
Time Complexity: O(N)
Auxiliary Space : O(N)
Approach 4: Using recursive method.
Python3
def occurrence_count(start,lst1,lst2,count):
if start = = len (lst1):
return count
if lst1[start] in lst2:
count + = 1
return occurrence_count(start + 1 ,lst1,lst2,count)
test_list1 = [ 1 , 3 , 4 , 5 , 1 , 4 , 4 , 6 , 7 ]
test_list2 = [ 4 , 6 , 1 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = occurrence_count( 0 ,test_list1,test_list2, 0 )
print ( "The occurrence count : " + str (res))
|
Output
The original list 1 is : [1, 3, 4, 5, 1, 4, 4, 6, 7]
The original list 2 is : [4, 6, 1]
The occurrence count : 6
Time Complexity: O(N)
Auxiliary Space : O(N)
Approach 5:Using Collections library
Python3
from collections import Counter
test_list1 = [ 1 , 3 , 4 , 5 , 1 , 4 , 4 , 6 , 7 ]
test_list2 = [ 4 , 6 , 1 ]
counts = Counter(test_list1)
res = sum (counts[ele] for ele in test_list2)
print ( "The occurrence count : " + str (res))
|
Output
The occurrence count : 6
Time Complexity: O(N)
Auxiliary Space : O(N)
Approach 6: Using defaultdict and sum
Python3
from collections import defaultdict
def get_occurrence_sum(list1, list2):
dict_count = defaultdict( int )
for i in list1:
dict_count[i] + = 1
return sum (dict_count[i] for i in list2)
test_list1 = [ 1 , 3 , 4 , 5 , 1 , 4 , 4 , 6 , 7 ]
test_list2 = [ 4 , 6 , 1 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = get_occurrence_sum(test_list1, test_list2)
print ( "The occurrence count : " + str (res))
|
Output
The original list 1 is : [1, 3, 4, 5, 1, 4, 4, 6, 7]
The original list 2 is : [4, 6, 1]
The occurrence count : 6
Time Complexity: O(N)
Auxiliary Space : O(N)
Explanation:
We create a defaultdict of integers and store the count of each element in the first list.
Then, we use sum function to get the sum of values corresponding to the keys in the second list.
This approach is efficient as the defaultdict stores the counts in constant time and sum function runs in linear time.
Approach 7: Using List Comprehension:
Python3
test_list1 = [ 1 , 3 , 4 , 5 , 1 , 4 , 4 , 6 , 7 ]
test_list2 = [ 4 , 6 , 1 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = len ([ele for ele in test_list1 if ele in test_list2])
print ( "The occurrence count : " + str (res))
|
Output
The original list 1 is : [1, 3, 4, 5, 1, 4, 4, 6, 7]
The original list 2 is : [4, 6, 1]
The occurrence count : 6
Time Complexity: O(N^2)
Auxiliary Space : O(1)
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...