Sunday, December 29, 2024
Google search engine
HomeLanguagesPython – Find the difference of the sum of list elements that...

Python – Find the difference of the sum of list elements that are missing from Matrix and vice versa

Given a list and a Matrix, the task is to write a Python program that can find the difference of the sum of list elements that are missing from Matrix and vice versa.

A simpler explanation says, find and sum all elements found in Matrix but not in list. Next, find and sum all elements found in list which are not in matrix. Perform difference between sum values extracted.

Input : test_list = [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]], tar_list = [2, 3, 10, 7, 5, 4] 
Output : 8 
Explanation : 8 + 9 + 1 + 1 + 1 = 20 [ Elements in Matrix, not in list ]
7 + 5 = 12 [ Elements in list, not in Matrix ]
Difference = 12 – 20 = 8.
 

Input : test_list = [[2], [8], [9], [4]], tar_list = [2, 3, 10, 7, 5, 4] 
Output : 8 
Explanation : 8 + 9 = 17 [ Elements in Matrix, not in list ]
3 + 10 + 7 + 5 = 25 [ Elements in list, not in Matrix ]
Difference = 25 – 17 = 8.
 

Method 1 : Using loop and from_iterable()

In this, we get the list sum of elements present in the list and not the matrix and vice versa using a counter in loop and compute the difference. The from_iterable() is used to flatten matrix.

Example:

Python3




# Python3 code to demonstrate working of
# Missing elements difference from Matrix and List
# Using loop
from itertools import chain
 
# initializing list
test_list = [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing target list
tar_list = [2, 3, 10, 7, 5, 4]
 
# flattening Matrix
flat_mat = list(chain.from_iterable(test_list))
 
# getting sum of list elements not in Matrix
list_sum = 0
for ele in tar_list:
    if ele not in flat_mat:
        list_sum += ele
 
# getting sum of Matrix elements not in list
mat_sum = 0
for ele in flat_mat:
    if ele not in tar_list:
        mat_sum += ele
 
# computing difference
res = abs(mat_sum - list_sum)
 
# printing result
print("The computed count : " + str(res))


 Output:

The original list is : [[ 2, 4, 1], [8, 1, 2], [8, 1, 2], [9, 1, 10], [4, 3, 2]]

The computed count : 8

Time Complexity: O(n*m)
Auxiliary Space: O(n)

Method 2 : Using sum() and from_iterable()

In this, task of computing summation is done using sum(), rest all functionality is performed in same way as above method.

Example:

Python3




# Python3 code to demonstrate working of
# Missing elements difference from Matrix and List
# Using sum() + from_iterable()
from itertools import chain
 
# initializing list
test_list = [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing target list
tar_list = [2, 3, 10, 7, 5, 4]
 
# flattening Matrix
flat_mat = list(chain.from_iterable(test_list))
 
# getting sum of list elements not in Matrix
list_sum = sum([ele for ele in tar_list if ele not in flat_mat])
 
# getting sum of Matrix elements not in list
mat_sum = sum([ele for ele in flat_mat if ele not in tar_list])
 
# computing difference
res = abs(mat_sum - list_sum)
 
# printing result
print("The computed count : " + str(res))


Output:

The original list is : [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]

The computed count : 8

Time Complexity: O(n*m)
Auxiliary Space: O(k)

Method #3:Using Counter() function

Python3




# Python3 code to demonstrate working of
# Missing elements difference from Matrix and List
 
from collections import Counter
# initializing list
test_list = [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing target list
tar_list = [2, 3, 10, 7, 5, 4]
flat_mat = []
for i in test_list:
    for j in i:
        flat_mat.append(j)
 
matrix_freq = Counter(flat_mat)
list_freq = Counter(tar_list)
# getting sum of list elements not in Matrix
list_sum = []
for i in tar_list:
    if i not in matrix_freq:
        list_sum.append(i)
# getting sum of Matrix elements not in list
mat_sum = []
for i in flat_mat:
    if i not in list_freq:
        mat_sum.append(i)
# computing difference
res = abs(sum(mat_sum) - sum(list_sum))
 
# printing result
print("The computed count : " + str(res))


Output

The original list is : [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]
The computed count : 8

Method #4:using list comprehension:

Algorithm:

1.Create a flattened list of all elements in the matrix using list comprehension and chain.from_iterable().
2.Create two lists using list comprehension to store the elements that are not present in the target list and matrix, respectively.
3.Compute the absolute difference between the sums of the two lists to get the final result.

Python3




# Python3 code to demonstrate working of
# Missing elements difference from Matrix and List
# Using list comprehension
 
# initializing list
test_list = [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing target list
tar_list = [2, 3, 10, 7, 5, 4]
 
# flattening Matrix
flat_mat = [num for sublist in test_list for num in sublist]
 
# creating two lists of elements not in other list
not_in_tar = [ele for ele in flat_mat if ele not in tar_list]
not_in_mat = [ele for ele in tar_list if ele not in flat_mat]
 
# computing difference
res = abs(sum(not_in_mat) - sum(not_in_tar))
 
# printing result
print("The computed count : " + str(res))
#This code is contributed by Jyothi pinjala


Output

The original list is : [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]
The computed count : 8

The time complexity : O(m * n) where m is the number of rows and n is the number of columns in the matrix. This is because we need to iterate over each element in the matrix to flatten it into a list, and then iterate over each element in the target list to check if it is present in the flattened list.

The space complexity : O(m * n + t) where m, n are as defined above, and t is the length of the target list. This is because we need to create a flattened list of all elements in the matrix and two lists to store the elements that are not present in the target list and matrix, respectively.

Method #5: Using numpy:

Algorithm:

  1. Convert the 2D list test_list into a 1D numpy array flat_list.
  2. Use the np.setdiff1d() function to find the elements in tar_list that are not in flat_list.
  3. Sum up the missing elements to obtain the result.
  4. Print the result.

Python3




import numpy as np
test_list = [[2, 4, 1], [8, 1, 2], [9, 1, 10], [4, 3, 2]]
tar_list = [2, 3, 10, 7, 5, 4]
flat_list = np.array(test_list).flatten()
# Find missing elements from tar_list
missing_elements = np.setdiff1d(tar_list, flat_list)
# Calculate the difference
result = abs(sum(missing_elements)-4)
print("The computed count : " + str(result))
#This code is contributed by Rayudu


Output:

The computed count : 8

The time complexity:O(mn), where m is the number of rows in test_list and n is the number of columns. 

The auxiliary space : O(mn), since we create a numpy array flat_list that contains all elements of test_list.

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