Python | Records Intersection
Last Updated :
06 Apr, 2023
Sometimes, while working with tuples, we can have a problem in which we need similar features of two records. This type of application can come in the Data Science domain. Let’s discuss certain ways in which this problem can be solved.
Method #1: Using set() + “&” operator This task can be performed using symmetric difference functionality offered by XOR operator over sets. The conversion to set is done by set().
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( set (test_tup1) & set (test_tup2))
print ( "The similar elements from tuples are : " + str (res))
|
Output :
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The similar elements from tuples are : (4, 5)
Method #2: Using intersection() + set() This is a method similar to the above method, the difference is that instead of & operator, we use inbuilt function to perform the task of filtering dissimilar elements.
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( set (test_tup1).intersection( set (test_tup2)))
print ( "The similar elements from tuples are : " + str (res))
|
Output :
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The similar elements from tuples are : (4, 5)
Time complexity: O(n), where n is the length of the longer tuple because we are using the intersection() method of sets to find the common elements.
Auxiliary space: O(n) because we are creating two sets, each of which contains the same number of elements as the corresponding tuple, and a third set to store the common elements.
Method #3: Using Counter
Python3
from collections import Counter
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple (x for x in test_tup1 if Counter(
test_tup1)[x] = = Counter(test_tup2)[x])
print ( "The similar elements from tuples are : " + str (res))
|
Output
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The similar elements from tuples are : (4, 5)
This approach uses the Counter class from the collections module to create a counter object for each tuple, and then uses a list comprehension to filter the elements of the first tuple that have the same count in both counter objects. It then converts the resulting list to a tuple. Time complexity is O(n) as it iterates through the first tuple once and creates two counter objects with a time complexity of O(n) each.
Method #4: Using list comprehension and the “in” operator
Use a list comprehension and the “in” operator to find the common elements between the two tuples.
- Create a tuple named test_tup1 containing the values (3, 4, 5, 6).
- Create another tuple named test_tup2 containing the values (5, 7, 4, 10).
- Use a list comprehension to create a new tuple named res containing the elements that are common to both test_tup1 and test_tup2.
- Iterate through each element x in test_tup1.
- Check if x is in test_tup2.
- If x is in test_tup2, add it to the res tuple.
- Convert the res tuple to a string and print it, along with a message stating that it contains the similar elements from the tuples.
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
res = tuple (x for x in test_tup1 if x in test_tup2)
print ( "The similar elements from tuples are : " + str (res))
|
Output
The similar elements from tuples are : (4, 5)
Time complexity: O(n*m), where n and m are the lengths of the two input tuples.
Auxiliary space: O(k), where k is the number of elements that are common to both tuples.
Method 5: Using built-in function filter() + lambda function
The filter() function takes two arguments, a lambda function and an iterable, and returns a filter object containing only the items from the iterable for which the lambda function returns True. In this case, the lambda function checks if the element of test_tup1 is also present in test_tup2.
- Two tuples test_tup1 and test_tup2 are initialized with some elements.
- The original tuples test_tup1 and test_tup2 are printed using the print() function and string concatenation.
- A new tuple res is created by applying the filter() function on test_tup1. The filter() function is passed a lambda function as its first argument which checks if the element of test_tup1 is also present in test_tup2. The filter() function returns a filter object containing only the common elements between test_tup1 and test_tup2. The tuple() function is applied on this filter object to convert it into a new tuple.
- The new tuple res containing the common elements between test_tup1 and test_tup2 is printed using the print() function and string concatenation.
- The program execution ends.
Python3
test_tup1 = ( 3 , 4 , 5 , 6 )
test_tup2 = ( 5 , 7 , 4 , 10 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( filter ( lambda x: x in test_tup2, test_tup1))
print ( "The similar elements from tuples are : " + str (res))
|
Output
The original tuple 1 : (3, 4, 5, 6)
The original tuple 2 : (5, 7, 4, 10)
The similar elements from tuples are : (4, 5)
Time complexity: This approach has a time complexity of O(n), where n is the length of the smaller tuple.
Auxiliary space: This approach requires O(n) space to store the common elements in a new tuple.
Similar Reads
Python | Intersection in Tuple Records Data
Sometimes, while working with data, we may have a problem in which we require to find the matching records between two lists that we receive. This is a very common problem and records usually occur as a tuple. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list com
7 min read
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
3 min read
Multiple Sets Intersection in Python
In this article, a List of sets is given our task is to write a program to perform their intersection using Python. Examples of finding the intersection of multiple setsInput : test_list = [{5, 3, 6, 7}, {1, 3, 5, 2}, {7, 3, 8, 5}, {8, 4, 5, 3}] Output : {3, 5} Explanation : 3 and 5 is present in al
2 min read
Python | Difference in Record Lists
Sometimes, while working with data, we may have a problem in which we require to find the difference records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list
5 min read
Python - Remove None Nested Records
Sometimes, while working with Python Records, can have problem in which we need to perform the removal of data which have all key's values as None in nested records. This kind of problem can have application in data preprocessing. Lets discuss certain ways in which this task can be performed. Method
4 min read
Python | Mutually different Records
Sometimes, while working with data, we may have a problem in which we require to find the unmatching records between two lists that we receive. This is a very common problem and records usually occurs as a tuple. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list
4 min read
Python - Occurrence counter in List of Records
Sometimes, while dealing with records we can have a problem in which we need count the occurrence of incoming digits corresponding to different characters/players in a game and compile them in a dictionary. This can have application in gaming and web development. Lets discuss a way in which this can
2 min read
Python - Remove Consecutive K element records
Sometimes, while working with Python records, we can have a problem in which we need to remove records on the basis of presence of consecutive K elements in tuple. This kind of problem is peculiar but can have applications in data domains. Let's discuss certain ways in which this task can be perform
7 min read
Python | Record Point with Minimum difference
Sometimes, while working with data, we might have a problem in which we need to find minimum difference between available pairs in list. This can be application to many problems in mathematics domain. Letâs discuss certain ways in which this task can be performed.Method #1 : Using min() + list compr
2 min read
Python - Convert Uneven Lists into Records
Sometimes, while working with Records, we can have a problem, that we have keys in one list and values in other. But sometimes, values can be multiple in order, like the scores or marks of particular subject. This type of problem can occur in school programming and development domains. Lets discuss
3 min read