Open In App

Python – Sublist Maximum in custom sliced List

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with data, we can have a problem in which we need to extract maximum not of whole list but of certain customly broken sublists. This kind of problem is peculiar and occurs in many domains. Let’s discuss certain ways in which in which this task can be performed. 

Method #1 : Using itertools.islice() + max() + list comprehension The combination of above methods can be used to solve this problem. In this islice() is used to perform custom slicing and list comprehension is used to iterate and max() is used to compute maximum. 

Python3




# Python3 code to demonstrate
# Sublist Maximum in custom sliced List
# using itertools.islice() + list comprehension
from itertools import islice
 
# initializing test list
test_list = [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
 
# initializing slice list
slice_list = [2, 1, 3, 4]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing slice list
print("The slice list : " + str(slice_list))
 
# using itertools.islice() + list comprehension
# Sublist Maximum in custom sliced List
temp = iter(test_list)
res = [max(list(islice(temp, part))) for part in slice_list]
 
# print result
print("The variable sliced list maximum is : " + str(res))


Output : 

The original list : [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
The slice list : [2, 1, 3, 4]
The variable sliced list maximum is : [5, 3, 10, 16]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(n), where n is the number of elements in the new res list 

Method #2 : Using zip() + accumulate() + max() + list slicing Apart from using the list comprehension to perform the task of binding, this method uses zip function to hold sublist element together, accumulate function joins the elements, and slicing is used to construct the required slicing. The max() is used to perform maximum. 

Python3




# Python3 code to demonstrate
# Sublist Maximum in custom sliced List
# using zip() + accumulate() + list slicing + max()
from itertools import accumulate
 
# initializing test list
test_list = [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
 
# initializing slice list
slice_list = [2, 1, 3, 4]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing slice list
print("The slice list : " + str(slice_list))
 
# using zip() + accumulate() + list slicing + max()
# Sublist Maximum in custom sliced List
res = [max(test_list[i - j: i]) for i, j in zip(accumulate(slice_list), slice_list)]
 
# print result
print("The variable sliced list maximum is : " + str(res))


Output : 

The original list : [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
The slice list : [2, 1, 3, 4]
The variable sliced list maximum is : [5, 3, 10, 16]

The time complexity of this approach is O(nm), where n is the length of the test_list and m is the length of the slice_list.

The auxiliary space complexity is O(n), as a list res is created with length equal to the length of the slice_list.

Method #3: Using a loop and slicing

Use a loop to iterate through the slice list and slices the original list accordingly to find the maximum value in each slice:

Python3




# initializing test list
test_list = [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
 
# initializing slice list
slice_list = [2, 1, 3, 4]
 
# Sublist Maximum in custom sliced List using a loop and slicing
res = []
start = 0
for part in slice_list:
    end = start + part
    sublist = test_list[start:end]
    res.append(max(sublist))
    start = end
 
# print result
print("The variable sliced list maximum is : " + str(res))


Output

The variable sliced list maximum is : [5, 3, 10, 16]

Time complexity: O(n^2), since it involves iterating through the slice list and slicing the original list for each element in the slice list. 
Auxiliary space: O(m), where m is the length of the slice list, since it only creates a new list to store the maximum values.

Method #5: Using numpy and np.split()

Step-by-step approach :

  • Initialize a list of numbers called test_list with some values: [1, 5, 3, 7, 8, 10, 11, 16, 9, 12].
  • Initialize another list of numbers called slice_list that specifies the size of each sublist we want to create from test_list: [2, 1, 3, 4].
  • Use np.cumsum(slice_list) to create a cumulative sum of the slice_list array: [2, 3, 6, 10].
  • Use np.cumsum(slice_list)[:-1] to exclude the last value of the cumulative sum array and create a new array [2, 3, 6].
  • Use np.split(test_list, [2, 3, 6]) to split the test_list array into sublists of sizes defined by slice_list.
  • Create a new list called res that will hold the maximum value of each sublist.
  • Loop through each sublist generated by the np.split function and find its maximum value using np.max(sublist). Append the maximum value to the res list.
  • Print the resulting list of maximum values.

Below is the implementation of the above approach:

Python3




import numpy as np
 
# initializing test list
test_list = [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
 
# initializing slice list
slice_list = [2, 1, 3, 4]
 
# Use np.split to split test_list into sublists
sublists = np.split(test_list, np.cumsum(slice_list)[:-1])
 
# Find the maximum value in each sublist
res = [np.max(sublist) for sublist in sublists]
 
# print result
print("The variable sliced list maximum is : " + str(res))


Output:

The variable sliced list maximum is : [5, 3, 10, 16]

Time complexity: O(n), where n is the length of the test_list. The np.split() function and list comprehension both iterate over the test_list once.
Auxiliary space: O(n). The np.split() function creates a new numpy array for each sublist, so the memory usage is proportional to the length of the test_list.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads