Python – Group Consecutive elements by Sign
Given a list group of consecutive elements on the basis of signs.
Input : test_list = [5, -3, 2, 4, 6, -2, -1, -7, -9, 2, 3]
Output : [[5], [-3], [2, 4, 6], [-2, -1, -7, -9], [2, 3]]
Explanation : Elements inserted into new list on sign change.
Input : test_list = [-2,3,4,5,6,-3]
Output : [[-2], [3, 4, 5, 6], [-3]]
Explanation : Elements inserted into new list on sign change.
Method #1: Using the loop
In this, whenever a sign(positive or negative) change occurs, a new list is initiated otherwise the elements are appended to a similar list as initialized.
Python3
test_list = [ 5 , - 3 , 2 , 4 , 6 , - 2 , - 1 , - 7 ,
- 9 , 2 , 3 , 10 , - 3 , - 5 , 3 ]
print ( "The original list is : " + str (test_list))
res = [[]]
for (idx, ele) in enumerate (test_list):
if ele ^ test_list[idx - 1 ] < 0 :
res.append([ele])
else :
res[ - 1 ].append(ele)
print ( "Elements after sign grouping : " + str (res))
|
Output
The original list is : [5, -3, 2, 4, 6, -2, -1, -7, -9, 2, 3, 10, -3, -5, 3]
Elements after sign grouping : [[5], [-3], [2, 4, 6], [-2, -1, -7, -9], [2, 3, 10], [-3, -5], [3]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using groupby() + list comprehension
In this, we perform the task of grouping using groupby(), and list comprehension is used to perform the task of iterating through the list. The condition for the sign is injected using the lambda function.
Python3
import itertools
test_list = [ - 2 , 3 , 4 , 5 , 6 , - 3 ]
print ( "The original list is : " + str (test_list))
res = [ list (ele) for idx, ele in itertools.groupby(test_list, lambda a: a > 0 )]
print ( "Elements after sign grouping : " + str (res))
|
Output
The original list is : [-2, 3, 4, 5, 6, -3]
Elements after sign grouping : [[-2], [3, 4, 5, 6], [-3]]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in groupby() + list comprehension which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), where n is the length of the input list as we’re using additional space other than the input list itself.
Method 3: using the numpy library
Steps:
- Import the numpy library.
- Initialize the input list test_list.
- Convert the input list to a numpy array using the numpy.array() method.
- Create a boolean array by checking if each element in the numpy array is greater than zero using the numpy.greater() method.
- Use the numpy.split() method to split the numpy array into sub-arrays based on the boolean array created in step 4.
- Convert each sub-array to a list using the tolist() method.
- Print the result.
Python3
import numpy as np
test_list = [ - 2 , 3 , 4 , 5 , 6 , - 3 ]
arr = np.array(test_list)
bool_arr = np.greater(arr, 0 )
res_arr = np.split(arr, np.where(bool_arr[: - 1 ] ! = bool_arr[ 1 :])[ 0 ] + 1 )
res = [i.tolist() for i in res_arr]
print ( "Elements after sign grouping : " + str (res))
|
OUTPUT :
Elements after sign grouping : [[-2], [3, 4, 5, 6], [-3]]
Time complexity: O(n)
Auxiliary space: O(n)
Last Updated :
04 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...