Python – Rows intersection with K

Sometimes while working with Python Matrix, we can have a problem in which we need to extract all the rows which match with other matrix, which has a specfic element. This kind of problem can occur in data domains as Matrix are input data types for many problems. Let’s discuss certain ways in which this task can be performed.

Input :
test_list1 = [[5, 6, 7], [7, 6, 6], [5, 7, 10]]
test_list2 = [[5, 6, 7], [7, 6, 8], [5, 7, 10]]
K = 7
Output : 2

Input :
test_list1 = [[6, 7], [6], [5]]
test_list2 = [[6, 7], [7], [5]]
K = 6
Output : 1

Method #1 : Using sum() + generator expression
The combination of above functions can be used to solve this problem. In this, we perform the task of counting match using sum(), and generator expression is used to perform the task of comparison.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Rows intersection with K
# Using sum() + generator expression
  
# initializing lists
test_list1 = [[5, 6, 7], [7, 6, 6], [5, 7, 10]]
test_list2 = [[5, 6, 7], [7, 6, 8], [5, 7, 10]]
  
# printing original list
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# initializing K 
K = 5
  
# Rows intersection with K
# Using sum() + generator expression
res = sum(sum(a == b for b in test_list2) for a in test_list1 if K in a)
  
# printing result 
print("The matched rows : " + str(res)) 

chevron_right


Output :



The original list 1 is : [[5, 6, 7], [7, 6, 6], [5, 7, 10]]
The original list 2 is : [[5, 6, 7], [7, 6, 8], [5, 7, 10]]
The matched rows : 2

 

Method #2 : Using Counter() + sum() + list comprehension
The combination of above functions can be an alternative to solve this problem. In this, we perform summation using sum() and Counter() is used to map rows from list2 to compare with list1 while counting frequency summation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Rows intersection with K
# Using Counter() + sum() + list comprehension
from collections import Counter
  
# initializing lists
test_list1 = [[5, 6, 7], [7, 6, 6], [5, 7, 10]]
test_list2 = [[5, 6, 7], [7, 6, 8], [5, 7, 10]]
  
# printing original list
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# initializing K 
K = 5
  
# Rows intersection with K
# Using Counter() + sum() + list comprehension
temp = Counter(tuple(b) for b in test_list2)
res = sum(temp[tuple(a)] for a in test_list1 if K in a)
  
# printing result 
print("The matched rows : " + str(res)) 

chevron_right


Output :

The original list 1 is : [[5, 6, 7], [7, 6, 6], [5, 7, 10]]
The original list 2 is : [[5, 6, 7], [7, 6, 8], [5, 7, 10]]
The matched rows : 2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.