Python – Check Similar elements in Matrix rows
Last Updated :
04 Apr, 2023
Given a Matrix and list, the task is to write a Python program to check if all the matrix elements of the row are similar to the ith index of the List.
Input : test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Output : True
Explanation : All rows have same elements.
Input : test_list = [[1, 1, 4], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Output : False
Explanation : All rows don't have same elements.
Method #1: Using loop
In this, we iterate through each row and check for its similar number in the list, if all the elements in a row are the same as an ith element in the List, true is returned, else false.
step-by-step approach :
- Initialize a matrix with some values using a nested list. The matrix is assigned to the variable test_list.
- Print the original matrix using the print() function.
- Initialize a list with some target values. The list is assigned to the variable tar_list.
- Initialize a boolean variable res to True which is used to store the result of the comparison.
- Initialize an integer variable flag to 0 which is used to break out of the outer loop if a mismatch is found.
- Loop through each index in the range of the length of the test_list. The variable idx is used to hold the current index value.
- Within the outer loop, loop through each element in the current row of test_list[idx]. The variable ele is used to hold the current element value.
- Check if the current element value is equal to the corresponding index element value in tar_list. If they are not equal, set res to False, set flag to 1, and break out of the inner loop.
- If flag is set to 1 (meaning a mismatch was found), break out of the outer loop as well.
- After the loops have completed, print the result of the comparison using the print() function.
Python3
test_list = [[ 1 , 1 , 1 ], [ 4 , 4 ],
[ 3 , 3 , 3 ], [ 5 , 5 , 5 , 5 ]]
print ( "The original list is : " + str (test_list))
tar_list = [ 1 , 4 , 3 , 5 ]
res = True
flag = 0
for idx in range ( len (test_list)):
for ele in test_list[idx]:
if ele ! = tar_list[idx]:
res = False
flag = 1
break
if flag:
break
print ("Are row index element similar\
to list index element ? : " + str (res))
|
Output:
The original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Are row index element similar to list index element ? : True
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method #2 : Using all() + generator expression
In this, we check for all elements to be equal using all(), and check for all elements follow this pattern using all() again. The iteration of elements and rows is done using generator expression.
Example:
Python3
test_list = [[ 1 , 1 , 1 ], [ 4 , 4 ],
[ 3 , 3 , 3 ], [ 5 , 5 , 5 , 5 ]]
print ( "The original list is : " + str (test_list))
tar_list = [ 1 , 4 , 3 , 5 ]
res = all ( all (ele = = tar_list[idx] for ele in test_list[idx])
for idx in range ( len (test_list)))
print ("Are row index element\
similar to list index element ? : " + str (res))
|
Output:
The original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Are row index element similar to list index element ? : True
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method #3 : Using count() and len() methods
Python3
test_list = [[ 1 , 1 , 1 ], [ 4 , 4 ],
[ 3 , 3 , 3 ], [ 5 , 5 , 5 , 5 ]]
print ( "The original list is : " + str (test_list))
tar_list = [ 1 , 4 , 3 , 5 ]
cnt = 0
res = False
for i in range ( 0 , len (tar_list)):
if (test_list[i].count(tar_list[i]) = = len (test_list[i])):
cnt + = 1
if (cnt = = len (test_list)):
res = True
print ( "Are row index element similar to list index element ? : " + str (res))
|
Output
The original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Are row index element similar to list index element ? : True
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), no space is required
Method #4 : Using operator.countOf() method
Python3
import operator as op
test_list = [[ 1 , 1 , 1 ], [ 4 , 4 ],
[ 3 , 3 , 3 ], [ 5 , 5 , 5 , 5 ]]
print ( "The original list is : " + str (test_list))
tar_list = [ 1 , 4 , 3 , 5 ]
cnt = 0
res = False
for i in range ( 0 , len (tar_list)):
if (op.countOf(test_list[i], tar_list[i]) = = len (test_list[i])):
cnt + = 1
if (cnt = = len (test_list)):
res = True
print ( "Are row index element similar to list index element ? : " + str (res))
|
Output
The original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Are row index element similar to list index element ? : True
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Method #5: Using set intersection
- Initialize Matrix and target list as given in the problem statement.
- Iterate through each row in the matrix.
- Convert each row into a set using the set() method.
- Find the intersection of the set and target list using the & operator.
- Check if the length of the intersection is equal to the length of the target list. If yes, then the elements in the row are similar to the target list.
- If all rows pass the similarity test, set the result variable to True, else set it to False.
- Print the result.
Example:
Python3
test_list = [[ 1 , 1 , 1 ], [ 4 , 4 ],
[ 3 , 3 , 3 ], [ 5 , 5 , 5 , 5 ]]
tar_list = [ 1 , 4 , 3 , 5 ]
print ( "The original list is : " + str (test_list))
res = True
for row in test_list:
if len ( set (row) & set (tar_list)) ! = len (tar_list):
res = False
break
print ( "Are row index element similar to list index element? : " + str (res))
|
Output
The original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Are row index element similar to list index element? : False
Time complexity: O(n*m), where n is the number of rows and m is the maximum length of a row in the matrix.
Auxiliary space: O(m), where m is the maximum length of a row in the matrix.
Method 6: Using map() and zip() functions
Step-by-step approach:
- Use the map() function to iterate over each row in the test_list
- Use the zip() function to iterate over the elements in the row and their corresponding elements in the tar_list
- Use the all() function to check if all elements in the row are equal to their corresponding elements in the tar_list
- If any row does not meet the condition, set res to False and break out of the loop
- Print the result
Python3
test_list = [[ 1 , 1 , 1 ], [ 4 , 4 ], [ 3 , 3 , 3 ], [ 5 , 5 , 5 , 5 ]]
print ( "The original list is : " + str (test_list))
tar_list = [ 1 , 4 , 3 , 5 ]
res = True
for row, tar in zip (test_list, tar_list):
if not all ( map ( lambda x, y: x = = tar, row, [tar] * len (row))):
res = False
break
print ( "Are row index element similar to list index element? : " + str (res))
|
Output
The original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Are row index element similar to list index element? : True
Time complexity: O(n*m), where n is the number of rows in the matrix and m is the length of the largest row
Auxiliary space: O(m), as we are only storing the tar_list for comparison.
Similar Reads
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 ta
6 min read
Python - Group similar elements into Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to perform grouping of all the elements with are the same. This kind of problem can have applications in data domains. Let's discuss certain ways in which this task can be performed. Input : test_list = [1, 3, 4, 4,
8 min read
Search Elements in a Matrix - Python
The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1
3 min read
Python - Remove Rows for similar Kth column element
Given a Matrix, remove row if similar element has occurred in row above in Kth column. Input : test_list = [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]], K = 2 Output : [[3, 4, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]] Explanation : In [2, 3, 5], we already has list [3, 4, 5] having 5 at K, i
7 min read
Python - Element wise Matrix Difference
Given two Matrixes, the task is to write a Python program to perform element-wise difference. Examples: Input : test_list1 = [[2, 4, 5], [5, 4, 2], [1, 2, 3]], test_list2 = [[6, 4, 6], [9, 6, 3], [7, 5, 4]] Output : [[4, 0, 1], [4, 2, 1], [6, 3, 1]] Explanation : 6 - 2 = 4, 4 - 4 = 0, 6 - 5 = 1. And
3 min read
Python - Similar index elements frequency
Sometimes, while working with Python list, we can have a problem in which we need to check if one element has similar index occurrence in other list. This can have possible application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using sum() + zip() The
7 min read
Python - Group Elements in Matrix
Given a Matrix with two columns, group 2nd column elements on basis of 1st column. Input : test_list = [[5, 8], [2, 0], [5, 4], [2, 3], [2, 9]] Output : {5: [8, 4], 2: [0, 3, 9]} Explanation : 8 and 4 are mapped to 5 in Matrix, all others to 2. Input : test_list = [[2, 8], [2, 0], [2, 4], [2, 3], [2
6 min read
Python | Row lengths in Matrix
The problems concerning matrix are quite common in both competitive programming and Data Science domain. One such problem that we might face is of finding the lengths of rows of matrix in uneven sized matrix. Let's discuss certain ways in which this problem can be solved. Method #1 : Using max() + m
4 min read
Python - Rows with all List elements
Given a Matrix, get all the rows with all the list elements. Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [1, 2] Output : [[2, 1, 8], [6, 1, 2]] Explanation : Extracted lists have 1 and 2. Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [2
8 min read
Python - Remove Similar Rows from Tuple Matrix
Sometimes, while working with Tuple Matrix, we can have a problem in which we get lots of data, which are similar, i.e elements are same in rows, just the ordering of tuples is different, it's sometimes, desired to get them removed. This kind of problem can have application in domains such as web de
9 min read