Open In App

Python | Remove similar element rows in tuple Matrix

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with data, we can have a problem in which we need to remove elements from the tuple matrix on a condition that if all elements in row of tuple matrix is same. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using list comprehension + all() This task can be performed using combination of above functions. In this, we traverse all rows using list comprehension and remove all elements that match the initial element in row’s column with help of all(). 

Python3




# Python3 code to demonstrate working of
# Remove similar element rows in tuple Matrix
# using list comprehension + all()
 
# initialize tuple
test_tup = ((1, 3, 5), (2, 2, 2),
            (9, 10, 10), (4, 4, 4))
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Remove similar element rows in tuple Matrix
# using list comprehension + all()
res = tuple(ele for ele in test_tup if not all(sub == ele[0] for sub in ele))
 
# printing result
print("The tuple after removal of like-element rows : " + str(res))


Output : 

The original tuple : ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4))
The tuple after removal of like-element rows : ((1, 3, 5), (9, 10, 10))

Time Complexity: O(n*n), where n is the length of the list test_tup
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 set() + generator expression 

This task can also be performed using the given functionalities. In this, we just check for length of reduced row using set() to be greater than 1. If yes, we know that it is the target row to be removed. 

Python3




# Python3 code to demonstrate working of
# Remove similar element rows in tuple Matrix
# using set() + generator expression
 
# initialize tuple
test_tup = ((1, 3, 5), (2, 2, 2),
            (9, 10, 10), (4, 4, 4))
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Remove similar element rows in tuple Matrix
# using set() + generator expression
res = tuple(ele for ele in test_tup if len(set(ele)) > 1)
 
# printing result
print("The tuple after removal of like-element rows : " + str(res))


Output : 

The original tuple : ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4))
The tuple after removal of like-element rows : ((1, 3, 5), (9, 10, 10))

Time complexity: O(nm), where n is the number of rows and m is the number of columns. 

Auxiliary space: O(n), where n is the number of rows. 

Method #3 : Using count() and len() methods

Python3




# Python3 code to demonstrate working of
# Remove similar element rows in tuple Matrix
 
# initialize tuple
test_tup = ((1, 3, 5), (2, 2, 2),
            (9, 10, 10), (4, 4, 4))
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Remove similar element rows in tuple Matrix
res=[]
for i in test_tup:
    if(i.count(i[0])!=len(i)):
        res.append(i)
res=tuple(res)
# printing result
print("The tuple after removal of like-element rows : " + str(res))


Output

The original tuple : ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4))
The tuple after removal of like-element rows : ((1, 3, 5), (9, 10, 10))

The time complexity of this program is O(n * m), where n is the number of rows and m is the number of columns in the tuple.

The auxiliary space complexity of this program is also O(n * m). The program initializes an empty list res to store the rows that don’t have all like elements. 

Method #4 : Using Counter() function

Python3




# Python3 code to demonstrate working of
# Remove similar element rows in tuple Matrix
from collections import Counter
# initialize tuple
test_tup = ((1, 3, 5), (2, 2, 2),
            (9, 10, 10), (4, 4, 4))
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Remove similar element rows in tuple Matrix
res = []
for i in test_tup:
    freq = Counter(i)
    if(len(freq) != 1):
        res.append(i)
res = tuple(res)
# printing result
print("The tuple after removal of like-element rows : " + str(res))


Output

The original tuple : ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4))
The tuple after removal of like-element rows : ((1, 3, 5), (9, 10, 10))

Time Complexity:O(n*n)

Auxiliary Space: O(n)

Method 5:  using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Remove similar element rows in tuple Matrix
import operator as op
# initialize tuple
test_tup = ((1, 3, 5), (2, 2, 2),
            (9, 10, 10), (4, 4, 4))
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Remove similar element rows in tuple Matrix
res = []
for i in test_tup:
    if(op.countOf(i, i[0]) != len(i)):
        res.append(i)
res = tuple(res)
# printing result
print("The tuple after removal of like-element rows : " + str(res))


Output

The original tuple : ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4))
The tuple after removal of like-element rows : ((1, 3, 5), (9, 10, 10))

Time Complexity: O(N*M)

Auxiliary Space : O(N*M)

Method#6: Using Recursive method.

The algorithm for the recursive method to remove similar element rows in a tuple matrix is as follows:

  1. Define a function remove_similar_rows that takes in one argument: test_tup.
  2. Check if test_tup is empty.
  3. If test_tup is empty, return an empty tuple.
  4. Otherwise, check if all elements in the first row of test_tup are equal to the first element of the first row.
  5. If all elements in the first row of test_tup are equal to the first element of the first row, call remove_similar_rows recursively with the remaining rows in test_tup.
  6. Otherwise, return a new tuple consisting of the first row in test_tup followed by the result of calling remove_similar_rows recursively with the remaining rows in test_tup.

Python3




def remove_similar_rows(test_tup):
    if not test_tup:
        return ()
    elif all(sub == test_tup[0][0] for sub in test_tup[0]):
        return remove_similar_rows(test_tup[1:])
    else:
        return (test_tup[0],) + remove_similar_rows(test_tup[1:])
 
test_tup = ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4))
 
print("The original tuple : " + str(test_tup))
res = remove_similar_rows(test_tup)
print("The tuple after removal of like-element rows : " + str(res))


Output

The original tuple : ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4))
The tuple after removal of like-element rows : ((1, 3, 5), (9, 10, 10))

The time complexity of this algorithm is O(n * m), where n is the number of rows and m is the number of columns in test_tup. This is because we need to iterate over all rows and columns in test_tup to remove similar element rows.

The auxiliary space of this algorithm is O(n), where n is the number of rows in test_tup. This is because we need to store n recursive calls on the call stack and create a new tuple to store the result.



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