Python | Remove similar element rows in tuple Matrix
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
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = tuple (ele for ele in test_tup if not all (sub = = ele[ 0 ] for sub in ele))
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
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = tuple (ele for ele in test_tup if len ( set (ele)) > 1 )
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
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = []
for i in test_tup:
if (i.count(i[ 0 ])! = len (i)):
res.append(i)
res = tuple (res)
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
from collections import Counter
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = []
for i in test_tup:
freq = Counter(i)
if ( len (freq) ! = 1 ):
res.append(i)
res = tuple (res)
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
import operator as op
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = []
for i in test_tup:
if (op.countOf(i, i[ 0 ]) ! = len (i)):
res.append(i)
res = tuple (res)
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:
- Define a function remove_similar_rows that takes in one argument: test_tup.
- Check if test_tup is empty.
- If test_tup is empty, return an empty tuple.
- Otherwise, check if all elements in the first row of test_tup are equal to the first element of the first row.
- 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.
- 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
Share your thoughts in the comments
Please Login to comment...