Python | Find overlapping tuples from list
Last Updated :
13 Apr, 2023
Sometimes, while working with tuple data, we can have a problem in which we may need to get the tuples which overlap a certain tuple. This kind of problem can occur in Mathematics domain while working with Geometry. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop In this method, we extract the pairs with overlap using conditional statements and append the suitable match into list keeping records.
Python3
test_list = [( 4 , 6 ), ( 3 , 7 ), ( 7 , 10 ), ( 5 , 6 )]
test_tup = ( 1 , 5 )
print ( "The original list : " + str (test_list))
res = []
for tup in test_list:
if (tup[ 1 ] > = test_tup[ 0 ] and tup[ 0 ] < = test_tup[ 1 ]):
res.append(tup)
print ( "The tuple elements that overlap the argument tuple is : "
+ str (res))
|
Output :
The original list : [(4, 6), (3, 7), (7, 10), (5, 6)] The tuple elements that overlap the argument tuple is : [(4, 6), (3, 7), (5, 6)]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2 : Using list comprehension This task can also be achieved using list comprehension functionality. This method is similar to the above one, just packed in one-liner for use as a shorthand.
Python3
test_list = [( 4 , 6 ), ( 3 , 7 ), ( 7 , 10 ), ( 5 , 6 )]
test_tup = ( 1 , 5 )
print ( "The original list : " + str (test_list))
res = [(idx[ 0 ], idx[ 1 ]) for idx in test_list
if idx[ 0 ] > = test_tup[ 0 ] and idx[ 0 ] < = test_tup[ 1 ]
or idx[ 1 ] > = test_tup[ 0 ] and idx[ 1 ] < = test_tup[ 1 ]]
print ( "The tuple elements that overlap the argument tuple is : "
+ str (res))
|
Output :
The original list : [(4, 6), (3, 7), (7, 10), (5, 6)] The tuple elements that overlap the argument tuple is : [(4, 6), (3, 7), (5, 6)]
Method 3: Using Brute Force:
Approach:
One approach to finding overlapping tuples from a list in Python is to use a brute force method, which involves comparing every tuple in the list to every other tuple in the list. This approach has a time complexity of O(n^2), where n is the number of tuples in the list. The space complexity is O(1), as we do not create any additional data structures.
Initialize an empty list to store the overlapping tuples.
Get the length of the input list tuples_list.
Loop over every pair of tuples in the input list using nested for loops:
a. Compare the intersection of the two sets of elements in the tuples using the & operator.
b. If the intersection is not empty, append the two tuples to the overlapping_tuples list.
Return the overlapping_tuples list.
Python3
def find_overlapping_tuples_brute_force(tuples_list):
overlapping_tuples = []
n = len (tuples_list)
for i in range (n):
for j in range (i + 1 , n):
if set (tuples_list[i]) & set (tuples_list[j]):
overlapping_tuples.append((tuples_list[i], tuples_list[j]))
return overlapping_tuples
tuples_list = [( 4 , 6 ), ( 3 , 7 ), ( 7 , 10 ), ( 5 , 6 )]
overlapping_tuples = find_overlapping_tuples_brute_force(tuples_list)
print (f "The original list : {tuples_list}" )
print (
f "The tuple elements that overlap the argument tuple is : {overlapping_tuples}" )
|
Output
The original list : [(4, 6), (3, 7), (7, 10), (5, 6)]
The tuple elements that overlap the argument tuple is : [((4, 6), (5, 6)), ((3, 7), (7, 10))]
Time complexity: O(n^2), as it involves two nested loops over the input list.
Auxiliary Space: is also O(n^2), as in the worst case, all pairs of tuples in the list could overlap with each other, resulting in a list of n(n-1)/2 tuples.
METHOD 4:Using List Comprehension.
APPROACH:
The list comprehension creates a new list of tuples that overlap with the given tuple by checking if the second element of the given tuple is greater than the first element of the current tuple in test_list, and if the first element of the given tuple is less than the second element of the current tuple in test_list.
ALGORITHM:
1.Initialize the input test_list as a list of tuples and test_tup as a tuple.
2.Define a list comprehension that iterates through each tuple in test_list.
3.For each tuple in test_list, check if it overlaps with the test_tup tuple by comparing their respective elements.
4.If the tuple overlaps with test_tup, add it to the overlapping_tuples list.
5.Print the list of overlapping tuples in the required format.
Python3
test_list = [( 4 , 6 ), ( 3 , 7 ), ( 7 , 10 ), ( 5 , 6 )]
test_tup = ( 1 , 5 )
overlapping_tuples = [tup for tup in test_list if test_tup[ 1 ] > tup[ 0 ] and test_tup[ 0 ] < tup[ 1 ]]
print ( "The tuple elements that overlap the argument tuple is :" , overlapping_tuples)
|
Output
The tuple elements that overlap the argument tuple is : [(4, 6), (3, 7)]
This approach has a time complexity of O(n), where n is the number of tuples in the list, since each tuple is checked only once.
The auxiliary space is O(k), where k is the number of overlapping tuples, since the list of overlapping tuples is stored in memory.
Share your thoughts in the comments
Please Login to comment...