Given a list of sequential numbers, Write a Python program to convert the given list into list of intervals.
Examples:
Input : [2, 3, 4, 5, 7, 8, 9, 11, 15, 16] Output : [[2, 5], [7, 11], [15, 16]] Input : [1, 2, 3, 6, 7, 8, 9, 10] Output : [[1, 3], [6, 10]]
Method #1 : Naive Approach
First, we use the brute force approach to Convert list of sequential number into intervals. Start a loop till the length of the list. In every iteration, use another loop to check the continuity of the sequence. As soon as the sequence stop, yield the lower and higher bound of each interval.
# Python3 program to Convert list of # sequential number into intervals def interval_extract(list): length = len(list) i = 0 while (i< length): low = list[i] while i <length-1 and list[i]+1 == list[i + 1]: i += 1 high = list[i] if (high - low >= 1): yield [low, high] elif (high - low == 1): yield [low, ] yield [high, ] else: yield [low, ] i += 1 # Driver code l = [2, 3, 4, 5, 7, 8, 9, 11, 15, 16] print( list(interval_extract(l))) |
[[2, 5], [7, 9], [11], [15, 16]]
Method #2 : Pythonic Naive
First, sort the given list. Initialize previous_number and range_start with first element. Start a loop and check if the next number is additive of the previous number, If yes, Initialize this number to previous number otherwise yield the new interval starting with range_start and ending with previous_number.
# Python3 program to Convert list of # sequential number into intervals def interval_extract(list): list = sorted(set(list)) range_start = previous_number = list[0] for number in list[1:]: if number == previous_number + 1: previous_number = number else: yield [range_start, previous_number] range_start = previous_number = number yield [range_start, previous_number] # Driver code l = [2, 3, 4, 5, 7, 8, 9, 11, 15, 16] print( list(interval_extract(l))) |
[[2, 5], [7, 9], [11, 11], [15, 16]]
Method #3 : Using itertools
The other pythonic method is to use Python itertools. We use itertools.groupby(). Where enumerate(iterable) is taken as iterable and lambda t: t[1] – t[0]) as key function to find the sequence for intervals.
# Python3 program to Convert list of # sequential number into intervals import itertools def intervals_extract(iterable): iterable = sorted(set(iterable)) for key, group in itertools.groupby(enumerate(iterable), lambda t: t[1] - t[0]): group = list(group) yield [group[0][1], group[-1][1]] # Driver code l = [2, 3, 4, 5, 7, 8, 9, 11, 15, 16] print( list(intervals_extract(l))) |
[[2, 5], [7, 9], [11, 11], [15, 16]]
Recommended Posts:
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python | Make pair from two list such that elements are not same in pairs
- Python program to check if the list contains three consecutive common numbers in Python
- Python | Maximum sum of elements of list in a list of lists
- Python | Sort the values of first list using second list
- Python List Comprehension | Segregate 0's and 1's in an array list
- Python | Pair and combine nested list to tuple list
- Python | Filter a list based on the given list of strings
- Python | Convert a nested list into a flat list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Sort list of list by specified index
- Python | Remove all values from a list present in other list
- Python | Sort list according to other list order
- Python | Convert list of strings and characters to list of characters
- Python | Insert list in another list
- Python | Convert a string representation of list into list
- Python | Ways to sum list of lists and return sum list
- Python | Convert list of string into sorted list of integer
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.
Improved By : ManasChhabra2

