Open In App

Python – Find minimum of non zero groups

Many times we need to get the minimum of not the whole list but just a part of it and at regular intervals. These intervals are sometimes decided statically before traversal, but sometimes, the constraint is more dynamic and hence we require to handle it in more complex way. Criteria discussed here is minimum of non-zero groups. Let’s discuss certain ways in which this task can be done. 

Method #1 : Using loops This task can be performed using the brute force manner using the loops. We just traverse the list each element to test for it’s succeeding element to be non-zero value and perform the minimum once we find a next value to be zero and append it in result list. 






# Python3 code to demonstrate
# Natural Numbers Minimum
# Using loops
 
# initializing list
test_list = [4, 9, 0, 0, 3, 4, 5, 0, 0, 4, 0]
 
# printing original list
print("The original list : " + str(test_list))
 
# using loops
# Natural Numbers Minimum
res = []
val = 99999
for ele in test_list:
    if ele == 0:
        if val != 99999:
            res.append(val)
            val = 99999
    else:
        val = min(val, ele)
 
# print result
print("The non-zero group Minimum of list is : " + str(res))

Output : 
The original list : [4, 9, 0, 0, 3, 4, 5, 0, 0, 4, 0]
The non-zero group Minimum of list is : [4, 3, 4]

Method #2: Using itertools.groupby() + min() This particular task can also be performed using groupby function to group all the non-zero values and min function can be used to perform their minimum. 






# Python3 code to demonstrate
# Natural Numbers Minimum
# Using itertools.groupby() + min()
from itertools import groupby
 
# initializing list
test_list = [4, 9, 0, 0, 3, 4, 5, 0, 0, 4, 0]
 
# printing original list
print("The original list : " + str(test_list))
 
# using itertools.groupby() + min()
# Natural Numbers Minimum
res = [min(val) for keys, val in groupby(test_list, key = lambda x: x != 0) if keys != 0]
 
# print result
print("The non-zero group minimum of list is : " + str(res))

Output : 
The original list : [4, 9, 0, 0, 3, 4, 5, 0, 0, 4, 0]
The non-zero group Minimum of list is : [4, 3, 4]

Method #3: Using a list comprehension to find non-zero groups:

Step-by-step approach:




def min_nonzero_groups_3(lst):
    groups = [list(group) for group in ''.join(map(str, lst)).split('0') if group]
    return [min(group) for group in groups]
 
# Example usage:
lst = [4, 9, 0, 0, 3, 4, 5, 0, 0, 4, 0]
print(min_nonzero_groups_3(lst))  # Output: [4, 3, 4]

Output
['4', '3', '4']

Time complexity: O(n)
Auxiliary space: O(n)

Method 4: Using the numpy library. 

Step-by-step approach:




# Python3 code to demonstrate
# Natural Numbers Minimum
# Using numpy
 
import numpy as np
 
# initializing list
test_list = [4, 9, 0, 0, 3, 4, 5, 0, 0, 4, 0]
 
# converting list to numpy array
arr = np.array(test_list)
 
# getting indices of non-zero elements
indices = np.nonzero(arr)[0]
 
# getting indices where consecutive elements differ by 1
diff_indices = np.where(np.diff(indices) != 1)[0]
 
# adding first and last indices
start_indices = np.append(indices[0], indices[diff_indices + 1])
end_indices = np.append(indices[diff_indices], indices[-1])
 
# getting minimum values for each group
min_vals = np.array([np.min(arr[start:end+1]) for start, end in zip(start_indices, end_indices)])
 
# print result
print("The non-zero group minimum of list is : " + str(list(min_vals)))

Output:

The non-zero group minimum of list is : [4, 3, 4]

Time complexity: O(n), where n is the length of the input list, because we are performing a constant number of operations for each non-zero group.
Auxiliary space: O(n), where n is the length of the input list, because we are storing the indices of non-zero elements and the start and end indices of each non-zero group.


Article Tags :