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 specific 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

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

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. sum() + generator expression performs n*n number of operations.
Auxiliary Space: O(1), constant extra space is required

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

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next