Python - Ranged Maximum Element in String List
Last Updated :
02 May, 2023
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
# Python3 code to demonstrate working of
# Ranged Maximum Element in String Matrix
# Using max() + split() + list comprehension
# initializing list
test_list = ['34, 78, 98, 23, 12',
'76, 65, 54, 43, 21',
'82, 45, 32, 45, 32',
'78, 34, 12, 34, 10']
# printing original list
print("The original list is : " + str(test_list))
# initializing Range
i, j = 2, 4
# Ranged Maximum Element in String Matrix
# Using max() + split() + list comprehension
res = max([max(idx.split(', ')[i - 1: j]) for idx in test_list])
# printing result
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
# Python3 code to demonstrate working of
# Ranged Maximum Element in String Matrix
# Using generator expression + max()
# initializing list
test_list = ['34, 78, 98, 23, 12',
'76, 65, 54, 43, 21',
'82, 45, 32, 45, 32',
'78, 34, 12, 34, 10']
# printing original list
print("The original list is : " + str(test_list))
# initializing Range
i, j = 2, 4
# Ranged Maximum Element in String Matrix
# Using generator expression + max()
res = max(ele for sub in test_list for ele in sub.split(', ')[i - 1: j])
# printing result
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
# Python3 code to demonstrate working of
# Ranged Maximum Element in String Matrix
# Using nested for loops
# initializing list
test_list = ['34, 78, 98, 23, 12',
'76, 65, 54, 43, 21',
'82, 45, 32, 45, 32',
'78, 34, 12, 34, 10']
# printing original list
print("The original list is : " + str(test_list))
# initializing Range
i, j = 2, 4
# Ranged Maximum Element in String Matrix
# Using nested for loops
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)
# printing result
print("The maximum ranged element : " + str(max_val))
OutputThe 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.
Similar Reads
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
Python - Maximum element in Cropped List Sometimes, while working with Python, we can have a problem in which we need to get maximum of list. But sometimes, we need to get this for between custom indices. This can be need of any domain be it normal programming or web development. Let's discuss certain ways in which this task can be perform
4 min read
Python - K Maximum elements with Index in List GIven a List, extract K Maximum elements with their indices. Input : test_list = [5, 3, 1, 4, 7, 8, 2], K = 2 Output : [(4, 7), (5, 8)] Explanation : 8 is maximum on index 5, 7 on 4th. Input : test_list = [5, 3, 1, 4, 7, 10, 2], K = 1 Output : [(5, 10)] Explanation : 10 is maximum on index 5. Method
4 min read
Python - Maximum of String Integer list Sometimes, while working with data, we can have a problem in which we receive a series of lists with data in string format, which we wish to find the max of each string list integer. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + int() This is the brute forc
4 min read
Python | Positions of maximum element in list Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python - Row with Maximum Record Element Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read
Python | Maximum Difference in String Sometimes, we might have a problem in which we require to get the maximum difference of 2 numbers from Strings but with a constraint of having the numbers in successions. This type of problem can occur while competitive programming. Letâs discuss certain ways in which this problem can be solved. Met
4 min read
Python - Elements Maximum till current index in List Given list with elements, extract element if it's the maximum element till current index. Input : test_list = [4, 6, 7, 8] Output : [4, 6, 7, 8] Explanation : All elements are maximum till their index. Input : test_list = [6, 7, 3, 6, 8, 7] Output : [7, 8] Explanation : 7 and 8 are maximum till thei
7 min read
Python - Maximum in Row Range Given a range and a Matrix, extract the maximum element out of that range of rows. Input : test_list = [[4, 3, 6], [9, 1, 3], [4, 5, 2], [9, 10, 3], [5, 9, 12], [3, 14, 2]], i, j = 2, 5 Output : 12 Explanation : Checks for rows 2, 3 and 4, maximum element is 12. Input : test_list = [[4, 3, 6], [9, 1
5 min read
Python - Sort Matrix by Maximum String Length Given a matrix, perform row sort basis on the maximum length of the string in it. Input : test_list = [['gfg', 'best'], ['geeksforgeeks'], ['cs', 'rocks'], ['gfg', 'cs']] Output : [['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks'], ['geeksforgeeks']] Explanation : 3 < 4 < 5 < 13, maximum leng
3 min read