Sometimes, while working with Python records, we can have a problem in which we need to extract one of the records that are equal to certain index, which is minimal of other index. This kind of problem occurs in domains such as web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [(‘Gfg’, 13, 5), (‘is’, 13, 6), (‘best’, 13, 2), (‘CS’, 13, 2)] eq_idx = 2, min_idx = 3 Output : [(‘best’, 13, 2)] Input : test_list = [(‘Gfg’, 12, 5), (‘is’, 12, 6), (‘best’, 13, 2), (‘CS’, 13, 3)] eq_idx = 2, min_idx = 3 Output : [(‘Gfg’, 12, 5), (‘best’, 13, 2)]
Method #1 : Using list comprehension + min() + lambda The combination of above functions can be used to solve this problem. In this, we use min() to extract the minimum index value, and list comprehension and lambda functions are used to perform the task of grouping elements.
Python3
# Python3 code to demonstrate working of # Equable Minimial Records # Using min() + list comprehension + lambda # initializing list test_list = [( 'Gfg' , 12 , 5 ), ( 'is' , 13 , 6 ), ( 'best' , 12 , 2 ), ( 'CS' , 13 , 2 )] # printing original list print ("The original list is : " + str (test_list)) # initializing Equate index eq_idx = 2 # initializing min index min_idx = 3 # Equable Minimial Records # Using min() + list comprehension + lambda res = [ min ((ele for ele in test_list if ele[eq_idx - 1 ] = = sub), key = lambda a: int (a[min_idx - 1 ])) for sub in {b[eq_idx - 1 ] for b in test_list}] # printing result print ("Equable Minimal Records : " + str (res)) |
The original list is : [('Gfg', 12, 5), ('is', 13, 6), ('best', 12, 2), ('CS', 13, 2)] Equable Minimal Records : [('best', 12, 2), ('CS', 13, 2)]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. list comprehension + min() + lambda performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using groupby() + filter() + lambda The combination of above functions can be used to solve this problem. In this, we perform the task of grouping using groupby() and filter() + lambda are used for task of matching conditions.
Python3
# Python3 code to demonstrate working of # Equable Minimial Records # Using groupby() + filter() + lambda from itertools import groupby # initializing list test_list = [( 'Gfg' , 12 , 5 ), ( 'is' , 13 , 6 ), ( 'best' , 12 , 2 ), ( 'CS' , 13 , 2 )] # printing original list print ("The original list is : " + str (test_list)) # initializing Equate index eq_idx = 2 # initializing min index min_idx = 3 # Equable Minimial Records # Using groupby() + filter() + lambda res = [] for k, val in groupby(test_list, lambda sub: sub[eq_idx - 1 ]): res.append( min ( filter ( lambda sub : sub[eq_idx - 1 ] = = k, test_list), key = lambda sub : sub[min_idx - 1 ])) res = list ( set (res)) # printing result print ("Equable Minimal Records : " + str (res)) |
The original list is : [('Gfg', 12, 5), ('is', 13, 6), ('best', 12, 2), ('CS', 13, 2)] Equable Minimal Records : [('best', 12, 2), ('CS', 13, 2)]
Method #3: Using a dictionary to group the records by the Equate index, and then finding the minimal record for each group using a loop.
Steps:
- Initialize a dictionary to group the records by the Equate index.
- Loop through the test_list and append each record to the corresponding group in the dictionary.
- Loop through each group in the dictionary and find the minimal record based on the min index.
- Append the minimal record to the result list.
- Print the result list.
Python3
# Python3 code to demonstrate working of # Equable Minimal Records # Using dictionary grouping + loop # initializing list test_list = [( 'Gfg' , 12 , 5 ), ( 'is' , 13 , 6 ), ( 'best' , 12 , 2 ), ( 'CS' , 13 , 2 )] # printing original list print ( "The original list is : " + str (test_list)) # initializing Equate index eq_idx = 2 # initializing min index min_idx = 3 # Equable Minimal Records # Using dictionary grouping + loop # initialize dictionary to group records by Equate index groups = {} for record in test_list: key = record[eq_idx - 1 ] if key in groups: groups[key].append(record) else : groups[key] = [record] # loop through each group and find minimal record based on min index res = [] for key in groups: group = groups[key] minimal_record = min (group, key = lambda x: x[min_idx - 1 ]) res.append(minimal_record) # printing result print ( "Equable Minimal Records : " + str (res)) |
The original list is : [('Gfg', 12, 5), ('is', 13, 6), ('best', 12, 2), ('CS', 13, 2)] Equable Minimal Records : [('best', 12, 2), ('CS', 13, 2)]
Time complexity: O(n log n) – since sorting is not used, the time complexity is O(n log n) due to the use of the built-in min() function inside the loop.
Auxiliary space: O(n) – since we are using a dictionary to group the records, the space complexity is proportional to the size of the input list.