I compared Python more-itertools 2.5 → 2.6

where more-itertools changed from 2.5 to 2.6

(Change) ilen

The implementation has changed from sum to deque. Why deque?

python3


from collections import deque
n = 10000
%timeit sum(1 for _ in range(n))                      # 2.5
%timeit deque(enumerate(range(n), 1), maxlen=1)[0][0] # 2.6
>>>
1000 loops, best of 3: 439 µs per loop
1000 loops, best of 3: 289 µs per loop

Indeed, it's getting faster.

(Add) divide

Divide into the specified number so that they are approximately the same size.

python3


from more_itertools import divide, always_iterable, adjacent, groupby_transform, context
[tuple(i) for i in divide(3, range(10))]
>>>
[(0, 1, 2, 3), (4, 5, 6), (7, 8, 9)]

(Addition) always_iterable

Iterable remains as it is, and non-iterable ones become iterable.

python3


always_iterable(None)
>>>
()

always_iterable(1)
>>>
(1,)

always_iterable([1,3,5])
>>>
[1, 3, 5]

(Addition) adjacent

Set True before and after where the conditions match.

python3


list(adjacent(lambda x: x==3, range(7)))
>>>
[(False, 0),
 (False, 1),
 (True, 2),
 (True, 3),
 (True, 4),
 (False, 5),
 (False, 6)]

list(adjacent(lambda x: x==3, range(7), distance=2)) #Up to 2 differences before and after
>>>
[(False, 0),
 (True, 1),
 (True, 2),
 (True, 3),
 (True, 4),
 (True, 5),
 (False, 6)]

(Add) groupby_transform

Convert keys and values after grouping.

python3


[(i,list(j)) for i,j in groupby_transform([1,1,3,2,2,2],
                                          lambda x: f'<{x}>', #Key conversion
                                          lambda x: x*10)]    #Value conversion
>>>
[('<1>', [10, 10]), ('<3>', [30]), ('<2>', [20, 20, 20])]

(Added) context (deleted in 3.0)

The one that can be used with with is a generator.

python3


with open(file name) as fp:
    print(fp.read())

↓ You can write like this

print(*[fp.read() for fp in context(open(file name))])

Reference: Introduction of itertools and more-itertools

that's all

Recommended Posts

I compared Python more-itertools 2.5 → 2.6
I compared Java and Python!
I started python
I compared python3 standard argparse and python-fire
I tried Python> autopep8
I compared "python dictionary type" and "excel function"
I implemented Python Logging
Relearn Python (Algorithm I)
I tried Python> decorator
Why I chose Python
I tried fp-growth with python
I tried scraping with Python
I wrote python in Japanese
curl -I python one liner
I made blackjack with python!
5 reasons I got into Python
[Python] I tried using OpenPose
I tried gRPC with Python
I tried scraping with python
I made blackjack with Python.
I compared blade and jinja2
What I learned in Python
I learned Python basic grammar
I compared Django's admin screens
I made wordcloud with Python.
I downloaded the python source
I compared the calculation time of the moving average written in Python
I made a Line-bot using Python!
I tried to touch Python (installation)
Python
I can't install python3 with pyenv-vertualenv
I checked Mac Python environment construction
[Introduction to Python] I compared the naming conventions of C # and Python.
I tried web scraping with python.
I can't remember Python regular expressions
I made my own Python library
Notation I encountered while learning Python
[Python] I implemented peripheral Gibbs sampling
I sent an SMS with Python
I compared Qiskit and Blueqat (beginner)
I wrote Fizz Buzz in Python
I took Progete's Python Learning Course I
I tried using Thonny (Python / IDE)
I tried Grumpy (Go running Python).
I liked the tweet with python. ..
I personally compared Java and Ruby
I can't install scikit-learn in Python
I compared Node.js and Python in creating thumbnails using AWS Lambda
I played with PyQt5 and Python3
I calculated "Levenshtein distance" using Python
I want to debug with Python
I tried running prolog with python 3.8.2.
I made a daemon with Python
I tried Line notification in Python
I tried SMTP communication with Python
[Python] I tried using YOLO v3
I wrote the stack in Python
I replaced the numerical calculation of Python with Rust and compared the speed
I put Python 2.7 in Sakura VPS 1GB.
I tried to summarize Python exception handling
I tried to implement PLSA in Python