Find the difference of the sum of list elements that are missing from Matrix and vice versa in python


In this article, we will learn how to find the difference of the sum of list elements that are missing from Matrix and vice versa.

Methods Used

The following are the various methods to accomplish this task −

  • Using for loop & from_iterable() function

  • Using sum() & from_iterable() functions

  • Using the Counter() function

Example

Assume we have taken an input matrix and a target list. We will now find the difference of the sum of list elements that are missing from Matrix and vice versa.

Input

inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]]
targetList = [9, 2, 1, 10, 3, 1]

Output

The absolute difference between matrix sum and list sum: 36

Explanation

In this example,

The list elements that are missing from the matrix are (9) so the sum is 9.

The matrix elements that are missing from the list are (6,5,4,8,15,7) so the sum is 45

The absolute difference between matrix sum and list sum = 45-9= 36

Method 1: Using for loop & from_iterable() function

itertools.chain.from_iterable() function

It belongs to the terminating iterators category.

The from_iterable() function returns a flattened iterable that contains all the elements of the input iterable and only accepts a single iterable as an argument. All of the input iterable's elements should also be iterable.

Syntax

chain.from_iterable(iterable)

Example

The following program returns the difference of the sum of list elements that are missing from the input Matrix and vice versa using for loop and from_iterable() function –

# importing chain from itertools module
from itertools import chain

# input matrix
inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]]

# printing input matrix
print("Input Matrix: ", inputMatrix)

# input target list
targetList = [9, 2, 1, 10, 3, 1]

# getting the flattened Matrix
flattenMatrix = list(chain.from_iterable(inputMatrix))

# storing the sum of target list elements that are not in the flattened matrix
listSum = 0

# travsering in the target list
for i in targetList:

   # checking whether the current element is not in a flattened matrix
   if i not in flattenMatrix:
      
      # adding that element to the list sum variable if the condition is true
      listSum += i
      
# storing the sum of matrix elements that are not in the target list
matrixSum = 0

# travsering in the flattened matrix
for i in flattenMatrix:
   
   # checking whether the current element is not in the target list
   if i not in targetList:
      
      # adding that element to matrix sum variable if the condition is true
      matrixSum += i

# getting the absolute difference between matrix sum and list sum
resultantDiff = abs(matrixSum - listSum)

# printing the resultant difference
print("Absolute difference between matrix sum and list sum:", resultantDiff)

Output

On executing, the above program will generate the following output –

Input Matrix: [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]]
Absolute difference between matrix sum and list sum: 36

Method 2: Using sum() & from_iterable() functions

sum() function − Returns the sum of all items in an iterable.

Example

The following program returns the difference of the sum of list elements that are missing from the input Matrix and vice versa using sum(), from_iterable() functions –

# importing chain from itertools module
from itertools import chain
inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]]
print("Input Matrix: ", inputMatrix)
targetList = [9, 2, 1, 10, 3, 1]

# getting the flattened matrix of the input matrix
flattenMatrix = list(chain.from_iterable(inputMatrix))

# getting the sum of target list elements that are not in the flattened Matrix
listSum = sum([i for i in targetList if i not in flattenMatrix])

# getting the sum of input matrix elements that are not in the target list
matrixSum = sum([i for i in flattenMatrix if i not in targetList])

# getting the absolute difference between matrix sum and list sum
resultantDiff = abs(matrixSum - listSum)
print("Absolute difference between matrix sum and list sum:", resultantDiff)

Output

Input Matrix: [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]]
Absolute difference between matrix sum and list sum: 36

Method 3: Using the Counter() function

Counter() function − a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked.

Example

The following program returns the difference of the sum of list elements that are missing from the input Matrix and vice versa using the Counter() function –

# importing Counter from the collections module
from collections import Counter
inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, 7, 2]]
targetList = [9, 2, 1, 10, 3, 1]

# empty list for storing flattened matrix
flattenMatrix = []

# traversing through the input matrix and getting its flattened matrix
for p in inputMatrix:
   for q in p:
      flattenMatrix.append(q)
      
# getting the frequency of the flattened matrix
matrixFreq = Counter(flattenMatrix)

# getting the frequency of the target list
listFreq = Counter(targetList)

# storing the sum of target list elements that are not in the flattened matrix
listSum = []

# traversing through the target list
for p in targetList:
   
   # checking whether the current element is not in a matrix frequency list
   if p not in matrixFreq:
   
      # appending that element to the list sum if the condition is true
      listSum.append(p)

# storing the sum of matrix elements that are not in the target list
matrixSum = []

# travsering in the flattened matrix
for p in flattenMatrix:
   
   # checking whether the current element is not in a list frequency list
   if p not in listFreq:
      
      # appending that element to the matrix sum
      matrixSum.append(p)

# getting the absolute difference between matrix sum and list sum
resultantDiff = abs(sum(matrixSum) - sum(listSum))
print("Absolute difference between matrix sum and list sum:", resultantDiff)

Output

Absolute difference between matrix sum and list sum: 36

Conclusion

This article taught us three different methods for determining the difference between the total number of list elements missing from the matrix and vice versa. We also learned how to use the chain.from_iterable() function to flatten the matrix or list of lists.

Updated on: 27-Jan-2023

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements