Open In App

Python – Test if all rows contain any common element with other Matrix

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two Matrices, check if all rows contain at least one element common with the same index row of other Matrix.

Input : test_list1 = [[5, 6, 1], [2, 4], [9, 3, 5]], test_list2 = [[9, 1, 2], [9, 8, 2], [3, 7, 10]] 
Output : True 
Explanation : 1, 2 and 3 are common elements in rows.

Input : test_list1 = [[5, 6, 1], [2, 4], [9, 2, 6]], test_list2 = [[9, 1, 2], [9, 8, 2], [3, 7, 6]] 
Output : True 
Explanation : 1, 2 and 6 are common elements in rows. 

Method #1 : Using loop + in operator

In this, we iterate each row of matrix, and check for any element matching in other list using in operator, if any element is found, we mark it true, if all rows are true, True is returned.

Python3




# Python3 code to demonstrate working of
# Test if all rows contain any common element with other Matrix
# Using loop "+" in operator
 
# initializing lists
test_list1 = [[5, 6, 1], [2, 4], [9, 3, 5]]
test_list2 = [[9, 1, 2], [9, 8, 2], [3, 7, 10]]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
res = True
for idx in range(0, len(test_list1)):
     
    temp = False
     
    # checking for any common element in 2nd list
    for ele in test_list1[idx]:
        if ele in test_list2[idx]:
            temp = True
            break
         
    # if any element not found, Result is false   
    if not temp :
        res = False
        break
 
# printing result
print("All row contain common elements with other Matrix : " + str(res))


Output:

The original list 1 is : [[5, 6, 1], [2, 4], [9, 3, 5]]
The original list 2 is : [[9, 1, 2], [9, 8, 2], [3, 7, 10]]
All row contain common elements with other Matrix : True

Time Complexity: O(n*n)
Auxiliary Space: O(1)

Method #2 : Using any() + loop

In this, one nested loop is avoided and the result is computed using any() for getting any common element in the 2nd Matrix.

Python3




# Python3 code to demonstrate working of
# Test if all rows contain any common element with other Matrix
# Using loop any() "+" loop
 
# initializing lists
test_list1 = [[5, 6, 1], [2, 4], [9, 3, 5]]
test_list2 = [[9, 1, 2], [9, 8, 2], [3, 7, 10]]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
res = True
for idx in range(0, len(test_list1)):
     
    # checking for common element in list 2 in same index
    temp = any(ele in test_list2[idx] for ele in test_list1[idx])
         
    # if any element not found, Result is false   
    if not temp :
        res = False
        break
 
# printing result
print("All row contain common elements with other Matrix : " + str(res))


Output:

The original list 1 is : [[5, 6, 1], [2, 4], [9, 3, 5]]
The original list 2 is : [[9, 1, 2], [9, 8, 2], [3, 7, 10]]
All row contain common elements with other Matrix : True

Time Complexity: O(n*m)
Auxiliary Space: O(k)

Method 3: using set intersection operation between rows of two matrices. 

Steps to implement this approach :

  • Initialize two sets to hold the elements of each row of both matrices.
  • Iterate through each row of the first matrix.
  • Convert the current row of the first matrix into a set.
  • Iterate through each row of the second matrix.
  • Compute the intersection between the set of the current row of the first matrix and the set of the current row of the second matrix.
  • If the intersection is not empty, it means that there is a common element between the two rows, so set the result to True and break out of the inner loop.
  • If no common element is found for a row of the first matrix, set the result to False and break out of the outer loop.
  • Print the final result.

Python3




# initializing lists
test_list1 = [[5, 6, 1], [2, 4], [9, 3, 5]]
test_list2 = [[9, 1, 2], [9, 8, 2], [3, 7, 10]]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
res = False
for row1 in test_list1:
    row1_set = set(row1)
    for row2 in test_list2:
        row2_set = set(row2)
        if row1_set.intersection(row2_set):
            res = True
            break
    if not res:
        break
 
# printing result
print("All row contain common elements with other Matrix : " + str(res))


Output

The original list 1 is : [[5, 6, 1], [2, 4], [9, 3, 5]]
The original list 2 is : [[9, 1, 2], [9, 8, 2], [3, 7, 10]]
All row contain common elements with other Matrix : True

The time complexity of this approach is O(n^2), where n is the length of the longest row in either matrix. 
The space complexity is O(m), where m is the length of the longest row in either matrix, due to the creation of the sets.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads