Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

Split an iterable into multiple using arbitrary predicates.

Project description

Split an iterable into multiple using arbitrary predicates.

This package comes with a single function: multisplitby.multi_split_by.

For all lists values and predicates, the following conditions are always true:

  1. 1 + len(predicates) = len(list(multi_split_by(values, predicates)))
  2. values == itertools.chain.from_iterable(multi_split_by(values, predicates))

Normal usage with one predicate:

>>> values = range(4)
>>> predicates = [lambda x: 2 < x]
>>> list(map(list, multi_split_by(values, predicates)))
[[0, 1, 2], [3]]

Normal usage with several predicates:

>>> values = range(9)
>>> predicates = [lambda x: 2 < x, lambda x: 4 < x, lambda x: 7 < x]
>>> list(map(list, multi_split_by(values, predicates)))
[[0, 1, 2], [3, 4], [5, 6, 7], [8]]

If no values are given, will result in |predicates| + 1 generators, all yielding empty lists.

>>> values = []
>>> predicates = [lambda x: 2 < x, lambda x: 4 < x, lambda x: 7 < x]
>>> list(map(list, multi_split_by(values, predicates)))
[[], [], [], []]

If no predicates are given, will result in a single generator that yields the original list:

>>> values = range(4)
>>> predicates = []
>>> list(map(list, multi_split_by(values, predicates)))
[[0, 1, 2, 3]]

Installation

Install from PyPI with:

$ pip install multisplitby

or get the latest code from GitHub with:

$ git clone https://github.com/cthoyt/multisplitby.git
$ cd multisplitby
$ pip install -e .

Project details


Release history Release notifications

This version
History Node

0.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
multisplitby-0.0.1-py3-none-any.whl (4.0 kB) Copy SHA256 hash SHA256 Wheel py3
multisplitby-0.0.1.tar.gz (5.6 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page