Thursday, December 26, 2024
Google search engine
HomeLanguagesPython – Sum of each List element occurrence in another

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




# Python3 code to demonstrate
# Sum of each List element occurrence in another
# using nested loops
 
# Initializing lists
test_list1 = [1, 3, 4, 5, 1, 4, 4, 6, 7]
test_list2 = [4, 6, 1]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Sum of each List element occurrence in another
# using nested loops
res = 0
for ele in test_list2:
    for ele1 in test_list1:
        if ele1 == ele:
            res = res + 1
 
# printing result
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




# Python3 code to demonstrate
# Sum of each List element occurrence in another
# using sum() + count()
 
# Initializing lists
test_list1 = [1, 3, 4, 5, 1, 4, 4, 6, 7]
test_list2 = [4, 6, 1]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Sum of each List element occurrence in another
# using sum() + count()
res = sum(test_list1.count(idx) for idx in test_list2)
 
# printing result
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




# Python3 code to demonstrate
# Sum of each List element occurrence in another
import operator as op
# Initializing lists
test_list1 = [1, 3, 4, 5, 1, 4, 4, 6, 7]
test_list2 = [4, 6, 1]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Sum of each List element occurrence in another
# using sum() + count()
res = sum(op.countOf(test_list1, idx) for idx in test_list2)
 
# printing result
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




# Python3 code to demonstrate
# Sum of each List element occurrence in another
#using recursive approach
def occurrence_count(start,lst1,lst2,count):
  if start==len(lst1):  #base condition
    return count
  if lst1[start] in lst2:   #checking if lst1[start] present in lst2 or not
    count+=1
  return occurrence_count(start+1,lst1,lst2,count)  #calling recursive call
 
# initializing lists
test_list1 = [1, 3, 4, 5, 1, 4, 4, 6, 7]
test_list2 = [4, 6, 1]
 
# printing original lists
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)
 
# printing result
print("The occurrence count : " + str(res))
#this code contributed by tvsk


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
 
# Initializing lists
test_list1 = [1, 3, 4, 5, 1, 4, 4, 6, 7]
test_list2 = [4, 6, 1]
 
# Count occurrences of elements in first list
counts = Counter(test_list1)
 
# Use a list comprehension to add up occurrences of elements in second list
res = sum(counts[ele] for ele in test_list2)
 
# printing result
print ("The occurrence count : " + str(res))
#This code is contributed Vinay Pinjala.


Output

The occurrence count : 6

Time Complexity: O(N)
Auxiliary Space : O(N)

Approach 6: Using defaultdict and sum
 

Python3




from collections import defaultdict
 
#Function to get the sum of each element in the first list
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)
 
#Initializing lists
test_list1 = [1, 3, 4, 5, 1, 4, 4, 6, 7]
test_list2 = [4, 6, 1]
 
#printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
#Get the sum of each element in the first list
res = get_occurrence_sum(test_list1, test_list2)
 
#printing result
print ("The occurrence count : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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




# Initializing lists
test_list1 = [1, 3, 4, 5, 1, 4, 4, 6, 7]
test_list2 = [4, 6, 1]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# Sum of each List element occurrence in another
# using List Comprehension
res = len([ele for ele in test_list1 if ele in test_list2])
# printing result
print ("The occurrence count : " + str(res))
#This code is contributed by Jyothi pinjala


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)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments