Open In App

Python | Records Intersection

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Records Intersection
# Using set() + "&" operator
 
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Records Intersection
# Using set() + "&" operator
res = tuple(set(test_tup1) & set(test_tup2))
 
# printing result
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




# Python3 code to demonstrate working of
# Records Intersection
# Using intersection() + set()
 
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Records Intersection
# Using intersection() + set()
res = tuple(set(test_tup1).intersection(set(test_tup2)))
 
# printing result
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
 
# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Records Intersection
# Using Counter
res = tuple(x for x in test_tup1 if Counter(
    test_tup1)[x] == Counter(test_tup2)[x])
 
# printing result
print("The similar elements from tuples are : " + str(res))
# This code is contributed by Edula Vinay Kumar Reddy


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




# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# find common elements
res = tuple(x for x in test_tup1 if x in test_tup2)
 
# printing result
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.

  1. Two tuples test_tup1 and test_tup2 are initialized with some elements.
  2. The original tuples test_tup1 and test_tup2 are printed using the print() function and string concatenation.
  3. 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.
  4. The new tuple res containing the common elements between test_tup1 and test_tup2 is printed using the print() function and string concatenation.
  5. The program execution ends.

Python3




# initialize tuples
test_tup1 = (3, 4, 5, 6)
test_tup2 = (5, 7, 4, 10)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Records Intersection using filter() and lambda function
res = tuple(filter(lambda x: x in test_tup2, test_tup1))
 
# printing result
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.



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads