Python – Find the difference of the sum of list elements that are missing from Matrix and vice versa
Last Updated :
24 Mar, 2023
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
from itertools import chain
test_list = [[ 2 , 4 , 1 ], [ 8 , 1 , 2 ], [ 9 , 1 , 10 ], [ 4 , 3 , 2 ]]
print ( "The original list is : " + str (test_list))
tar_list = [ 2 , 3 , 10 , 7 , 5 , 4 ]
flat_mat = list (chain.from_iterable(test_list))
list_sum = 0
for ele in tar_list:
if ele not in flat_mat:
list_sum + = ele
mat_sum = 0
for ele in flat_mat:
if ele not in tar_list:
mat_sum + = ele
res = abs (mat_sum - list_sum)
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
from itertools import chain
test_list = [[ 2 , 4 , 1 ], [ 8 , 1 , 2 ], [ 9 , 1 , 10 ], [ 4 , 3 , 2 ]]
print ( "The original list is : " + str (test_list))
tar_list = [ 2 , 3 , 10 , 7 , 5 , 4 ]
flat_mat = list (chain.from_iterable(test_list))
list_sum = sum ([ele for ele in tar_list if ele not in flat_mat])
mat_sum = sum ([ele for ele in flat_mat if ele not in tar_list])
res = abs (mat_sum - list_sum)
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
from collections import Counter
test_list = [[ 2 , 4 , 1 ], [ 8 , 1 , 2 ], [ 9 , 1 , 10 ], [ 4 , 3 , 2 ]]
print ( "The original list is : " + str (test_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)
list_sum = []
for i in tar_list:
if i not in matrix_freq:
list_sum.append(i)
mat_sum = []
for i in flat_mat:
if i not in list_freq:
mat_sum.append(i)
res = abs ( sum (mat_sum) - sum (list_sum))
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
test_list = [[ 2 , 4 , 1 ], [ 8 , 1 , 2 ], [ 9 , 1 , 10 ], [ 4 , 3 , 2 ]]
print ( "The original list is : " + str (test_list))
tar_list = [ 2 , 3 , 10 , 7 , 5 , 4 ]
flat_mat = [num for sublist in test_list for num in sublist]
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]
res = abs ( sum (not_in_mat) - sum (not_in_tar))
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
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:
- Convert the 2D list test_list into a 1D numpy array flat_list.
- Use the np.setdiff1d() function to find the elements in tar_list that are not in flat_list.
- Sum up the missing elements to obtain the result.
- 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()
missing_elements = np.setdiff1d(tar_list, flat_list)
result = abs ( sum (missing_elements) - 4 )
print ( "The computed count : " + str (result))
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...