Python – Replace Non-Maximum Records
Last Updated :
09 Mar, 2023
Sometimes, while working with Python records, we can have a problem in which we need to perform replace of all the records whose one element is not Maximum. This kind of problem can have application in many domains including day-day programming and web development domain. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [(1, 4), (9, 11)] K = “Non-Max” Output : [‘Non-Max’, (9, 11)] Input : test_list = [(9, 11), (9, 11), (9, 11)] K = “Non-Max” Output : [(9, 11), (9, 11), (9, 11)]
Method #1 : Using loop + map() + filter() + lambda The combination of above functionalities can be used to perform this task. In this, we perform task of filtering using map() and filter function, after that allocation of values to non-maximum elements using brute force approach using loop.
Python3
test_list = [( 1 , 4 ), ( 9 , 11 ), ( 4 , 6 ), ( 6 , 8 ), ( 9 , 11 )]
print ("The original list is : " + str (test_list))
K = None
res = []
temp = list ( filter ( lambda ele: ele = = max (test_list), test_list))
for ele in test_list:
if ele not in temp:
res.append(K)
else :
res.append(ele)
print ("The list after replacing Non - Maximum : " + str (res))
|
Output :
The original list is : [(1, 4), (9, 11), (4, 6), (6, 8), (9, 11)]
The list after replacing Non-Maximum : [None, (9, 11), None, None, (9, 11)]
Method #2 : Using list comprehension + map() + filter() + lambda The combination of above functions can be used to perform this task. In this, we perform task using similar to above method, just difference being using list comprehension for allotting K.
Python3
test_list = [( 1 , 4 ), ( 9 , 11 ), ( 4 , 6 ), ( 6 , 8 ), ( 9 , 11 )]
print ("The original list is : " + str (test_list))
K = None
temp = list ( filter ( lambda ele: ele = = max (test_list), test_list))
res = [ele if ele in temp else K for ele in test_list]
print ("The list after replacing Non - Maximum : " + str (res))
|
Output :
The original list is : [(1, 4), (9, 11), (4, 6), (6, 8), (9, 11)]
The list after replacing Non-Maximum : [None, (9, 11), None, None, (9, 11)]
Method 3: Use a for loop to iterate through the list and keep track of the maximum value, then use a list comprehension to replace all non-maximum values with None.
we first find the maximum value in the list using the built-in max() function. Then, we iterate through the list using a for loop and replace each non-maximum value with None using a list comprehension. Finally, we print the resulting list.
Python3
test_list = [( 1 , 4 ), ( 9 , 11 ), ( 4 , 6 ), ( 6 , 8 ), ( 9 , 11 )]
print ( "The original list is: " + str (test_list))
max_val = max (test_list)
res = [max_val if ele = = max_val else None for ele in test_list]
print ( "The list after replacing Non-Maximum: " + str (res))
|
Output
The original list is: [(1, 4), (9, 11), (4, 6), (6, 8), (9, 11)]
The list after replacing Non-Maximum: [None, (9, 11), None, None, (9, 11)]
Time complexity: O(n), where n is the length of the input list. This is because we need to iterate through the input list once to find the maximum value and once again to create the output list.
Auxiliary space: O(n), where n is the length of the input list. This is because we are creating a new list res that has the same length as the input list, and we are storing the maximum value in the variable max_val.
Share your thoughts in the comments
Please Login to comment...