Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Check Similar elements in Matrix rows

Python – Check Similar elements in Matrix rows

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 :

  1. Initialize a matrix with some values using a nested list. The matrix is assigned to the variable test_list.
  2. Print the original matrix using the print() function.
  3. Initialize a list with some target values. The list is assigned to the variable tar_list.
  4. Initialize a boolean variable res to True which is used to store the result of the comparison.
  5. Initialize an integer variable flag to 0 which is used to break out of the outer loop if a mismatch is found.
  6. 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.
  7. 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.
  8. 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.
  9. If flag is set to 1 (meaning a mismatch was found), break out of the outer loop as well.
  10. After the loops have completed, print the result of the comparison using the print() function.

Python3




# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using loop
 
# initializing Matrix
test_list = [[1, 1, 1], [4, 4],
             [3, 3, 3], [5, 5, 5, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing tar_list
tar_list = [1, 4, 3, 5]
 
res = True
flag = 0
for idx in range(len(test_list)):
    for ele in test_list[idx]:
 
        # checking for row index
        # equal to list index elements
        if ele != tar_list[idx]:
            res = False
            flag = 1
            break
 
    if flag:
        break
 
# printing result
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




# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using all() + generator expression
 
# initializing Matrix
test_list = [[1, 1, 1], [4, 4],
             [3, 3, 3], [5, 5, 5, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing tar_list
tar_list = [1, 4, 3, 5]
 
# nested all() used to check each element and rows
res = all(all(ele == tar_list[idx] for ele in test_list[idx])
          for idx in range(len(test_list)))
 
# printing result
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




# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using loop
 
# initializing Matrix
test_list = [[1, 1, 1], [4, 4],
             [3, 3, 3], [5, 5, 5, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing tar_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
 
# printing result
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




# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using loop
 
import operator as op
# initializing Matrix
test_list = [[1, 1, 1], [4, 4],
             [3, 3, 3], [5, 5, 5, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing tar_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
 
# printing result
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

  1. Initialize Matrix and target list as given in the problem statement.
  2. Iterate through each row in the matrix.
  3. Convert each row into a set using the set() method.
  4. Find the intersection of the set and target list using the & operator.
  5. 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.
  6. If all rows pass the similarity test, set the result variable to True, else set it to False.
  7. Print the result.

Example:

Python3




# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using set intersection
 
# initializing Matrix and target list
test_list = [[1, 1, 1], [4, 4],
             [3, 3, 3], [5, 5, 5, 5]]
tar_list = [1, 4, 3, 5]
 
# printing original list
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
 
# printing result
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:

  1. Use the map() function to iterate over each row in the test_list
  2. Use the zip() function to iterate over the elements in the row and their corresponding elements in the tar_list
  3. Use the all() function to check if all elements in the row are equal to their corresponding elements in the tar_list
  4. If any row does not meet the condition, set res to False and break out of the loop
  5. Print the result

Python3




# initializing Matrix
test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing tar_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
 
# printing result
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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments