Given Tuples list and search list consisting of tuples to search, our task is to write a Python Program to extract indices of matching tuples.
Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 2)]
Output : [3, 1]
Explanation : (3, 4) from search list is found on 3rd index on test_list, hence included in result.
Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 0)]
Output : [3, 1, 2]
Explanation : (3, 4) from search list is found on 3rd index on test_list, hence included in result.
Method #1 : Using lookup dictionary + enumerate() + list comprehension
In this, a lookup dictionary is formed to map all the tuples with matching one’s indices. Then lookup dictionary is used to get indices of mapped tuples as result using list comprehension.
step by step approach:
- Initialize a list of tuples named test_list containing some tuples.
- Print the original list of tuples using the print() function and str() function to convert the list into a string.
- Initialize another list of tuples named search_tup containing some tuples.
- Create a lookup dictionary named lookup_dict using a dictionary comprehension where the keys are the tuples in test_list and the values are their indices.
- Create a result list named res using a list comprehension that loops through each tuple idx in search_tup and checks if idx is present in the lookup_dict using the if idx in lookup_dict condition. If it is present, then the value of the corresponding key in the lookup_dict is appended to the res list.
- Print the resulting list of tuple indices using the print() function and str() function to convert the list into a string.
Python3
test_list = [( 4 , 5 ), ( 7 , 6 ), ( 1 , 0 ), ( 3 , 4 )]
print ( "The original list is : " + str (test_list))
search_tup = [( 3 , 4 ), ( 8 , 9 ), ( 7 , 6 ), ( 1 , 2 )]
lookup_dict = {val: key for key,val in enumerate (test_list)}
res = [lookup_dict[idx] for idx in search_tup if idx in lookup_dict]
print ( "The match tuple indices : " + str (res))
|
Output:
The original list is : [(4, 5), (7, 6), (1, 0), (3, 4)]
The match tuple indices : [3, 1]
Time complexity: O(n) – where n is the length of the test_list or search_tup, whichever is larger. The creation of the lookup_dict has a time complexity of O(n), and the list comprehension has a time complexity of O(k) where k is the number of matches found in search_tup.
Auxiliary space: O(n) – where n is the length of the test_list. The lookup_dict takes up O(n) space.
Method #2 : Using list comprehension + enumerate()
In this, we perform the task of getting indices using enumerate(), and list comprehension is used for the task of iteration of all the elements of tuple and matching for equality.
Python3
test_list = [( 4 , 5 ), ( 7 , 6 ), ( 1 , 0 ), ( 3 , 4 )]
print ( "The original list is : " + str (test_list))
search_tup = [( 3 , 4 ), ( 8 , 9 ), ( 7 , 6 ), ( 1 , 2 )]
res = [idx for idx, val in enumerate (test_list) for ele in search_tup if ele = = val]
print ( "The match tuple indices : " + str (res))
|
Output:
The original list is : [(4, 5), (7, 6), (1, 0), (3, 4)]
The match tuple indices : [1, 3]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #3 : Using index() method
Python3
test_list = [( 4 , 5 ), ( 7 , 6 ), ( 1 , 0 ), ( 3 , 4 )]
print ( "The original list is : " + str (test_list))
search_tup = [( 3 , 4 ), ( 8 , 9 ), ( 7 , 6 ), ( 1 , 2 )]
res = []
for i in search_tup:
if i in test_list:
res.append(test_list.index(i))
print ( "The match tuple indices : " + str (res))
|
Output
The original list is : [(4, 5), (7, 6), (1, 0), (3, 4)]
The match tuple indices : [3, 1]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using operator.countOf() method
Python3
import operator as op
test_list = [( 4 , 5 ), ( 7 , 6 ), ( 1 , 0 ), ( 3 , 4 )]
print ( "The original list is : " + str (test_list))
search_tup = [( 3 , 4 ), ( 8 , 9 ), ( 7 , 6 ), ( 1 , 2 )]
res = []
for i in search_tup:
if op.countOf(test_list,i)> 0 :
res.append(test_list.index(i))
print ( "The match tuple indices : " + str (res))
|
Output
The original list is : [(4, 5), (7, 6), (1, 0), (3, 4)]
The match tuple indices : [3, 1]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 5: using the filter() function
Python3
test_list = [( 4 , 5 ), ( 7 , 6 ), ( 1 , 0 ), ( 3 , 4 )]
print ( "The original list is : " + str (test_list))
search_tup = [( 3 , 4 ), ( 8 , 9 ), ( 7 , 6 ), ( 1 , 2 )]
res = list ( filter ( lambda x: test_list[x[ 0 ]] in search_tup, enumerate (test_list)))
res = [i for i, _ in res]
print ( "The match tuple indices : " + str (res))
|
Output
The original list is : [(4, 5), (7, 6), (1, 0), (3, 4)]
The match tuple indices : [1, 3]
Time complexity: O(n), where n is the length of the list test_list.
Auxiliary space: O(n) as well, since we are storing the indices of the matching tuples in the res list.
Method 6: Using the map function and a lambda function:
We can use the map function to apply a lambda function to each tuple in search_tup. The lambda function checks if the tuple exists in test_list using the in operator and returns the index using the index method if it does, and None otherwise. We then convert the map object to a list and filter out the None values using a list comprehension.
Python3
test_list = [( 4 , 5 ), ( 7 , 6 ), ( 1 , 0 ), ( 3 , 4 )]
search_tup = [( 3 , 4 ), ( 8 , 9 ), ( 7 , 6 ), ( 1 , 2 )]
res = list ( map ( lambda x: test_list.index(
x) if x in test_list else None , search_tup))
res = [i for i in res if i is not None ]
print ( "The match tuple indices : " + str (res))
|
Output
The match tuple indices : [3, 1]
Time complexity: O(mn), where m is the length of search_tup and n is the length of test_list. .
Auxiliary space: O(m), where m is the length of search_tup.
Method 7: Using nested loops and tuple comparison.
Algorithm:
- Initialize an empty list res to store the indices of matching tuples.
- Iterate over each tuple tup in the search_tup list.
- Iterate over each tuple lst_tup in the test_list list.
- Check if tup is equal to lst_tup. If they are equal, append the index of lst_tup to res and break the inner loop.
- After both loops have completed, print the list of indices res.
Python3
test_list = [( 4 , 5 ), ( 7 , 6 ), ( 1 , 0 ), ( 3 , 4 )]
search_tup = [( 3 , 4 ), ( 8 , 9 ), ( 7 , 6 ), ( 1 , 2 )]
res = []
for i in range ( len (search_tup)):
for j in range ( len (test_list)):
if search_tup[i] = = test_list[j]:
res.append(j)
break
print ( "The match tuple indices : " + str (res))
|
Output
The match tuple indices : [3, 1]
Time Complexity:
The time complexity of this algorithm is O(N*M), where N is the length of the search_tup list and M is the length of the test_list list.
This is because we are iterating over each tuple in search_tup and checking it against each tuple in test_list.
In the worst-case scenario, where no tuples match, we will iterate over all N*M possible combinations of tuples.
However, in the best-case scenario, where the first tuple in search_tup matches the first tuple in test_list, we only need to iterate over one combination of tuples.
In general, the time complexity of this algorithm depends on the specific input data and the distribution of matching tuples between the two lists.
Space Complexity:
The space complexity of this algorithm is O(1), as we are only using a fixed amount of memory to store the variables res, i, j, tup, and lst_tup.
We do not create any new lists or dictionaries, so the space required by the algorithm does not depend on the size of the input data.
Please Login to comment...