[PYTHON] Put together consecutive values in the list

Make a note of the concise description method of summarizing the sequence of monotonically increasing numbers for each consecutive value.

import itertools

sample = [1,2,3,5,6,7,9,10]

[list(g) for _, g in itertools.groupby(
    sample, key=lambda n, c=itertools.count(): n - next(c))]

# => [[1, 2, 3], [5, 6, 7], [9, 10]]

[ʻItertools.groupby](https://docs.python.org/3.9/library/itertools.html#itertools.groupby) passes each element of the iterable of the first argument to the second argument and returns. Returns an iterator that returns consecutive elements with the same value, along with the elements passed to the second argument function. In the above example, the first argument is sample and the second argument keyis a lambda expression. Lambda expressions have two arguments,n and optional c, but c has a default value of ʻitertools.count (), which is continuous in groupby. When called when determining the identity of the elements to be used, each element of sample is passed to n. ʻItertools.count ()produces an iterator that returns a number incrementing by1 from 0. The default value of c is executed only once when the lambda expression is defined, and an iterator that returns the number + 1in order from 0 is assigned. So, forn --next (c), next (c) increases by 1with each call, son increases by 1and the same value is obtained from this lambda expression. It will be returned, but a different value will be returned ifn increases more than 1. The iterator that returns consecutive elements with the same return value of this lambda expression is g, and list (g) `generates a list of consecutive elements in a nested list by list comprehension notation.

Recommended Posts

Put together consecutive values in the list
Methods available in the list
[Python] How to output the list values in order
Put the lists together in pandas to make a DataFrame
Put the module in the Pepper app
[python] Get the rank of the values in List in ascending / descending order
OR the List in Python (zip function)
Change the list in a for statement
How to handle consecutive values in MySQL
Get the EDINET code list in Python
[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
Get only the subclass elements in a list
Put the second axis in 2dhistgram of matplotlib
In print, the non-ascii characters in the list look great
Search by the value of the instance in the list
[Python] How to remove duplicate values from the list
Extract and list personal names and place names in the text
Complement the library you put in anaconda with jedi-vim
[python] Get the list of classes defined in the module
Read the linked list in csv format with graph-tool
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
Sorted list in Python
Put matplotlib in Centos7.
Put scipy in ec2
FizzBuzz in list comprehension
Put jedi in emacs 24
Filter List in Python
List find in Python
Swapping values in Python
Put pip in Blender
Sort and output the elements in the list as elements and multiples in Python.
Get the number of specific elements in a python list
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
Get the number of occurrences for each element in the list
Create a new list by combining duplicate elements in the list
I want to make the Dictionary type in the List unique
Find the average / standard deviation of the brightness values in the image