Python | Find groups of strictly increasing numbers in a list

Given a list of integers, write a Python program to find groups of strictly increasing numbers.

Examples:

Input : [1, 2, 3, 5, 6]
Output : [[1, 2, 3], [5, 6]]

Input : [8, 9, 10, 7, 8, 1, 2, 3]
Output : [[8, 9, 10], [7, 8], [1, 2, 3]]

Approach #1 : Pythonic naive



This is a naive approach which uses an extra input list space. It makes use of a for loop and in every iteration, it checks if the next element increments from previous by 1. If yes, append it to the current sublist, otherwise, create another sublist.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Find groups 
# of strictly increasing numbers within 
  
def groupSequence(lst):
    res = [[lst[0]]]
  
    for i in range(1, len(lst)):
        if lst[i-1]+1 == lst[i]:
            res[-1].append(lst[i])
  
        else:
            res.append([lst[i]])
    return res
      
# Driver program 
l = [8, 9, 10, 7, 8, 1, 2, 3]
print(groupSequence(l))

chevron_right


Output:

[[8, 9, 10], [7, 8], [1, 2, 3]]

 
Approach #2 : Alternate naive

This is an alternative to the above mentioned naive approach. This method is quite simple and straight. It constructs a start_bound list and an end_bound list, which contains the position of starting and ending sequence of increasing integers. Thus simply return the bounds using for loops.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Find groups 
# of strictly increasing numbers within 
  
def groupSequence(l):
    start_bound = [i for i in range(len(l)-1)
        if (l == 0 or l[i] != l[i-1]+1)
        and l[i + 1] == l[i]+1]
  
    end_bound = [i for i in range(1, len(l))
        if l[i] == l[i-1]+1 and
        (i == len(l)-1 or l[i + 1] != l[i]+1)]
  
    return [l[start_bound[i]:end_bound[i]+1]
    for i in range(len(start_bound))]
      
# Driver program 
l = [8, 9, 10, 7, 8, 1, 2, 3]
print(list(groupSequence(l)))

chevron_right


Output:

[[8, 9, 10], [7, 8], [1, 2, 3]]

 
Approach #3 : Using iterable and yield
This approach uses another list ‘res’ and an iterable ‘it’. A variable ‘prev’ is used for keeping the record of previous integer and start is used for getting the starting position of the increasing sequence. Using a loop, in every iteration, we check if start element is a successor of prev or not. If yes, we append it to res, otherwise, we simply yield the res + [prev] as list element.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Find groups 
# of strictly increasing numbers within 
  
def groupSequence(x):
    it = iter(x)
    prev, res = next(it), []
  
    while prev is not None:
        start = next(it, None)
  
        if prev + 1 == start:
            res.append(prev)
        elif res:
            yield list(res + [prev])
            res = []
        prev = start
      
# Driver program 
l = [8, 9, 10, 7, 8, 1, 2, 3]
print(list(groupSequence(l)))

chevron_right


Output:

[[8, 9, 10], [7, 8], [1, 2, 3]]

 
Approach #4 : Using itertools
Python itertools provides operations like cycle and groupby which are used in this method. First we form another list ‘temp_list‘ using cycle. Cycle generates an infinitely repeating series of values. Then we group the temp_list accordingly using groupby operation and finally yield the desired output.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Find groups 
# of strictly increasing numbers within 
from itertools import groupby, cycle
  
def groupSequence(l):
    temp_list = cycle(l)
  
    next(temp_list)
    groups = groupby(l, key = lambda j: j + 1 == next(temp_list))
    for k, v in groups:
        if k:
            yield tuple(v) + (next((next(groups)[1])), )
  
      
# Driver program 
l = [8, 9, 10, 7, 8, 1, 2, 3]
print(list(groupSequence(l)))

chevron_right


Output:

[(8, 9, 10), (7, 8), (1, 2, 3)]


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.