Python – Ranged Maximum Element in String List
Sometimes, while working with Python data, we can have a problem in which we have data in form of String List and we require to find the maximum element in that data, but that also in a certain range of indices. This is quite peculiar problem but can have application in data domains. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [’34, 78, 98, 23, 12′, ’76, 65, 54, 43, 21′] i, j = 1, 3 Output : 76 Input : test_list = [’34, 78, 98, 23, 12′, ’76, 65, 54, 43, 21′] i, j = 3, 5 Output : 98
Method #1 : Using max() + split() + list comprehension The combination of above functions is used to solve this problem. In this, we perform the split of each strings element in list, in a particular range, and then max() is used to find the maximum element in that range across each list. First, maximum amongst the sublist and then amongst the other indices.
Python3
test_list = [ '34, 78, 98, 23, 12' ,
'76, 65, 54, 43, 21' ,
'82, 45, 32, 45, 32' ,
'78, 34, 12, 34, 10' ]
print ("The original list is : " + str (test_list))
i, j = 2 , 4
res = max ([ max (idx.split( ', ' )[i - 1 : j]) for idx in test_list])
print ("The maximum ranged element : " + str (res))
|
Output :
The original list is : [’34, 78, 98, 23, 12′, ’76, 65, 54, 43, 21′, ’82, 45, 32, 45, 32′, ’78, 34, 12, 34, 10′] The maximum ranged element : 98
Time Complexity: O(n*n) where n is the number of elements in the in the list “test_list”. The max() + split() + list comprehension is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the in the list “test_list”.
Method #2 : Using generator expression + max() The combination of above functionalities can be used to solve this problem. In this, we extract all the elements maximum using single max() function and use nested generator expression to extract all elements for strings as once.
Python3
test_list = [ '34, 78, 98, 23, 12' ,
'76, 65, 54, 43, 21' ,
'82, 45, 32, 45, 32' ,
'78, 34, 12, 34, 10' ]
print ("The original list is : " + str (test_list))
i, j = 2 , 4
res = max (ele for sub in test_list for ele in sub.split( ', ' )[i - 1 : j])
print ("The maximum ranged element : " + str (res))
|
Output :
The original list is : [’34, 78, 98, 23, 12′, ’76, 65, 54, 43, 21′, ’82, 45, 32, 45, 32′, ’78, 34, 12, 34, 10′] The maximum ranged element : 98
Method #3: Using nested for loops
Initialize a variable max_val to -1, which will store the maximum value found in the given range.
Use two nested for loops to iterate over each element in the range (i to j) of each row of the list.
Convert each element to an integer using the int() function and compare it with max_val. If it is greater than max_val, update max_val.
Return max_val as the maximum ranged element.
Python3
test_list = [ '34, 78, 98, 23, 12' ,
'76, 65, 54, 43, 21' ,
'82, 45, 32, 45, 32' ,
'78, 34, 12, 34, 10' ]
print ( "The original list is : " + str (test_list))
i, j = 2 , 4
max_val = - 1
for row in test_list:
row_vals = row.split( ', ' )[i - 1 :j]
for val in row_vals:
if int (val) > max_val:
max_val = int (val)
print ( "The maximum ranged element : " + str (max_val))
|
Output
The original list is : ['34, 78, 98, 23, 12', '76, 65, 54, 43, 21', '82, 45, 32, 45, 32', '78, 34, 12, 34, 10']
The maximum ranged element : 98
Time complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary space: O(1), as we are only storing a single variable to keep track of the maximum value.
Last Updated :
02 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...