A function that divides iterable into N pieces in Python

Iterable is divided into N pieces and returned as a generator.

I improved it by referring to the link provided in the comment. It's shortened to 3 lines and can handle an infinite list. Split iterator into chunks with python

python


import itertools
def splitparN(iterable, N=3):
    for i, item in itertools.groupby(enumerate(iterable), lambda x: x[0] // N):
        yield (x[1] for x in item)


for x in splitparN(range(12)):
    print(tuple(x))
"""
output:
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
(9, 10, 11)
"""

eternal = libs.splitparN(itertools.cycle("hoge"))
print([tuple(next(eternal)) for x in range(4)])
#output:[('h', 'o', 'g'), ('e', 'h', 'o'), ('g', 'e', 'h'), ('o', 'g', 'e')]

#itertools.Can be undone with chain
print(tuple(itertools.chain.from_iterable(splitparN(range(12)))))
#output:(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)

Recommended Posts

A function that divides iterable into N pieces in Python
Create a function in Python
A function that measures the processing time of a method in python
Precautions when pickling a function in python
The eval () function that calculates a string as an expression in python
Draw a graph of a quadratic function in Python
A memo that I wrote a quicksort in Python
To execute a Python enumerate function in JavaScript
Get the caller of a function in Python
A program that removes duplicate statements in Python
[Python, PyPDF2] A script that divides a spread PDF into two left and right
I made a familiar function that can be used in statistics with Python
Python that merges a lot of excel into one excel
What's in that variable (when running a Python script)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Until you insert data into a spreadsheet in Python
A function that easily calculates a listwise removal tree (Python)
A Python program that converts ical data into text
MALSS, a tool that supports machine learning in Python
What does the last () in a function mean in Python?
A memo that implements the job of loading a GCS file into BigQuery in Python
I made a tool in Python that right-clicks an Excel file and divides it into files for each sheet.
Take a screenshot in Python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
Published a library that hides character data in Python images
Use callback function in Python
Loop through a generator that returns a date iterator in Python
ntile (decile) function in python
Let's create a script that registers with Ideone.com in Python.
I tried "a program that removes duplicate statements in Python"
Make a bookmarklet in Python
To return char * in a callback function using ctypes in Python
Create code that outputs "A and pretending B" in python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
Nonlinear function modeling in Python
Draw implicit function in python
[python] Manage functions in a dictionary (command table, function table, function pointer)
Immediate function in python (lie)
A memo of writing a basic function in Python using recursion
Draw a heart in Python
Try running a function written in Python using Fn Project
A set of script files that do wordcloud in Python3
[Python] A story that seemed to fall into a rounding trap
What I learned and coded for a function that opens a special Windows folder in Python3 ctypes
I thought about the logic that divides / 22 or / 23 into / 24 units by specifying the IP range in Python.
Play a sound in Python assuming that the keyboard is a piano keyboard
A memo that handles double-byte double quotes in Python regular expressions
How to make a string into an array or an array into a string in Python
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
Easy! Implement a Twitter bot that runs on Heroku in Python
[Python / Django] Create a web API that responds in JSON format
A record that GAMEBOY could not be done in Python. (PYBOY)
[Python] Execution time when a function is entered in a dictionary value
About psd-tools, a library that can process psd files in Python
I wrote a function to load a Git extension script in Python
A program that determines whether a number entered in Python is a prime number
A class that summarizes frequently used methods in twitter api (python)
What's new in datetime that is a bit more useful in Python 3
[Python] What is a zip function?