Source

python-split /

Filename Size Date modified Message
1.0 KB
924 B
782 B
5.7 KB

python-split -- functions to split and partition sequences

Installation

pip install split

Usage

To partition a sequence into chunks of equal size, use chop:

>>> from split import chop
>>> list(chop(3, range(10)))
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

If truncate=True keyword argument is given, sequence length is truncated to a multiple of chunk size, and all chunks have the same size.

>>> list(chop(3, range(10), truncate=True))
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]

To split a sequence into two by a given predicate, use partition:

>>> from split import partition
>>> def odd(x): return x%2
>>> map(list, partition(odd, range(5)))
[[1, 3], [0, 2, 4]]

To break a sequence into chunks on some separator elements:

>>> list(split(0, [1,2,3,0,4,5,0,0,6]))
[[1, 2, 3], [4, 5], [], [6]]