Skip to content
Related Articles

Related Articles

Python – Rows intersection with K
  • Last Updated : 02 Jun, 2020

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.




# 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)) 
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.




# 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)) 
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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :