Open In App

Python | Non-Repeating value Summation in Matrix

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes we need to find the unique values in a list, which is comparatively easy and its summation has been discussed earlier. But we can also get a matrix as input i.e a list of lists, finding unique in them are discussed in this article. Let’s see certain ways in which this can be achieved. 

Method #1: Using set() + list comprehension + sum() 

The set function can be used to convert the individual list to a non-repeating element list and the list comprehension is used to iterate to each of the lists. The task of performing summation is performed using sum(). 

  1. Initialize the matrix test_matrix as [[1, 3, 1], [4, 5, 3], [1, 2, 4]].
  2. Print the original matrix using the print() function and string concatenation.
  3. Use a list comprehension to flatten the matrix by iterating over each row j in test_matrix, and then over each element i in the row j, i.e., [i for j in test_matrix for i in j].
  4. Convert the flattened list to a set using the set() function to eliminate duplicates.
  5. Convert the resulting set back to a list using the list() function.
  6. Compute the sum of the resulting list using the sum() function, and assign the result to the variable res.
  7. Print the unique values summation in the matrix by using the print() function and string concatenation, with the value of res converted to a string using the str() function.

Python3




# Python3 code to demonstrate
# Non-Repeating value Summation in Matrix
# set() + list comprehension + sum()
 
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
 
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
 
# using set() + list comprehension + sum()
# Non-Repeating value Summation in Matrix
res = sum(list(set(i for j in test_matrix for i in j)))
 
# printing result
print("Unique values summation in matrix are : " + str(res))


Output : 

The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Unique values summation in matrix are : 15

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #2: Using chain() + set() + sum() 

The chain function performs a similar task that a list comprehension performs but in a faster way as it uses iterators for its internal processing and hence faster. The task of performing summation is performed using sum(). 

Python3




# Python3 code to demonstrate
# Non-Repeating value Summation in Matrix
# chain() + set() + sum()
from itertools import chain
 
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
 
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
 
# using chain() + set() + sum()
# Non-Repeating value Summation in Matrix
res = sum(list(set(chain(*test_matrix))))
 
# printing result
print("Unique values summation in matrix are : " + str(res))


Output : 

The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Unique values summation in matrix are : 15

Time Complexity: O(n*n) where n is the number of elements in the test_list. The chain() + set() + sum()  is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.

Method #3: Using extend()+sum()+list()+set() methods

Python3




# Python3 code to demonstrate
# Non-Repeating value Summation in Matrix
 
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
 
# printing the original matrix
print ("The original matrix is : " + str(test_matrix))
 
x=[]
for i in test_matrix:
    x.extend(i)
res=sum(list(set(x)))
# printing result
print ("Unique values summation in matrix are : " + str(res))


Output

The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Unique values summation in matrix are : 15

Time complexity: O(n^2), where n is the size of the matrix, because the code iterates over each element in the matrix to flatten it and then iterates over each unique element to compute the sum.
Auxiliary space: O(n), where n is the number of unique elements in the matrix.

Method #4: Using numpy.unique() + numpy.sum()

NumPy is a popular Python library for scientific computing that provides efficient numerical operations on large arrays. The numpy.unique() function can be used to find the unique values in a matrix, and numpy.sum() can be used to calculate the sum of these values.

Python3




import numpy as np
 
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
 
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
 
# using numpy.unique() + numpy.sum()
# Non-Repeating value Summation in Matrix
res = np.sum(np.unique(test_matrix))
 
# printing result
print("Unique values summation in matrix are : " + str(res))


Output:

The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Unique values summation in matrix are : 15

Time complexity: O(N*logN), where N is the total number of elements in the matrix.
Auxiliary space: O(N), where N is the total number of elements in the matrix, due to the creation of a new array to hold the unique values.

Method #5: Using a nested for loop and a set() to find unique elements and sum them up

 Use a nested for loop to iterate through each element in the matrix and add it to a set, which keeps only unique elements. Finally, the sum() function is used to sum up the unique elements.

Python3




# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
 
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
 
unique_set = set()
for row in test_matrix:
    for element in row:
        unique_set.add(element)
 
# Summing up unique elements
res = sum(unique_set)
 
# printing result
print("Unique values summation in matrix are : " + str(res))


Output

The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Unique values summation in matrix are : 15

Time complexity: O(N^2), where N is the number of elements in the matrix. This is because we are iterating through each element in the matrix, and the nested for loop takes O(N^2) time.

Auxiliary Space: O(N), where N is the number of unique elements in the matrix. This is because we are using a set to store the unique elements, which can take up to O(N) space.



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads