Python Program that prints rows from the matrix that have same element at a given index
Last Updated :
15 May, 2023
Given a Matrix, the following article shows how rows which has similar digit at the specified index will be extracted and returned as output.
Input : test_list = [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49],
[98, 34, 23]],
K = 1
Output : [[3345, 6355, 83, 938], [192, 993, 49]]
Explanation : 3 and 9 [ same ] in 1st column.
Input : test_list = [[3445, 6355, 83, 938], [323, 923, 845], [192, 993, 49],
[98, 34, 23]],
K = 1
Output : [[192, 993, 49]]
Explanation : 9 in 1st column.
Method 1 : Using all() and list comprehension
In this, we check for all the digits at a specified index using all(), and list comprehension is used for iterating each row.
Python3
test_list = [[ 3345 , 6355 , 83 , 938 ], [
323 , 923 , 845 ], [ 192 , 993 , 49 ], [ 98 , 34 , 23 ]]
print ( "The original list is : " + str (test_list))
K = 1
res = [row for row in test_list if all (
str (i)[K] = = str (row[ 0 ])[K] for i in row)]
print ( "Filtered Rows : " + str (res))
|
Output
The original list is : [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]]
Filtered Rows : [[3345, 6355, 83, 938], [192, 993, 49]]
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method 2 : Using filter() ,lambda and all()
In this, we check for all the rows for similar K column digits, filtering using filter() and lambda function. Similar to above method, all() does the task for checking each element.
Python3
test_list = [[ 3345 , 6355 , 83 , 938 ], [
323 , 923 , 845 ], [ 192 , 993 , 49 ], [ 98 , 34 , 23 ]]
print ( "The original list is : " + str (test_list))
K = 1
res = list ( filter ( lambda row: all (
str (i)[K] = = str (row[ 0 ])[K] for i in row), test_list))
print ( "Filtered Rows : " + str (res))
|
Output
The original list is : [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]]
Filtered Rows : [[3345, 6355, 83, 938], [192, 993, 49]]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in filter() ,lambda and all() which all has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space other than the input list itself.
Method 3: Using a nested for loop and an if statement
Python3
test_list = [[ 3345 , 6355 , 83 , 938 ], [ 323 , 923 , 845 ], [ 192 , 993 , 49 ], [ 98 , 34 , 23 ]]
print ( "The original list is : " + str (test_list))
K = 1
result = []
for row in test_list:
match = True
for i in row:
if str (i)[K] ! = str (row[ 0 ])[K]:
match = False
break
if match:
result.append(row)
print ( "Filtered Rows: " , result)
|
Output
The original list is : [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]]
Filtered Rows: [[3345, 6355, 83, 938], [192, 993, 49]]
Time complexity: O(mn), where m is the number of rows in the test_list and n is the number of elements in each row. This is because for each row, we loop through all elements in the row and check the Kth digit of each element. The all() function also takes O(n) time, so the overall time complexity is O(mn).
Auxiliary space: O(m), because we store the filtered rows in a new list, and the size of the new list is proportional to the number of rows that match the criteria. The space complexity is O(m) because we are storing m filtered rows.
Method 4: Using map() and zip() functions
Python3
test_list = [[ 3345 , 6355 , 83 , 938 ], [ 323 , 923 , 845 ], [ 192 , 993 , 49 ],
[ 98 , 34 , 23 ]]
K = 1
result = []
for row in test_list:
kth_digits = list ( map ( lambda x: str (x)[K], row))
if all (kth_digit = = str (row[ 0 ])[K] for kth_digit in kth_digits):
result.append(row)
print ( "Filtered Rows: " , result)
|
Output
Filtered Rows: [[3345, 6355, 83, 938], [192, 993, 49]]
Time complexity: O(n*m), where n is the number of rows and m is the length of the longest row.
Auxiliary space: O(k), where k is the length of the longest row.
Share your thoughts in the comments
Please Login to comment...