Skip to main content

PartitionWhile

The function PartitionWhile splits a collection into sublists comprised of consecutive elements which satisfy a given condition.

The first argument of PartitionWhile is a list to be partitioned. The second argument is a boolean function which determines the condition which the partitions must satisfy. The resulting sublists are such that this function evaluates to True on each of them.

For example:

from partition_while import PartitionWhile

print(PartitionWhile([1,2,3,4,5,6,7,8,9,10], lambda x: sum(x) <= 10))
# [[1, 2, 3, 4], [5], [6], [7], [8], [9], [10]]

In detail, the algorithm of PartitionWhile implements a double loop for each element of the list and its successive elements. The first subpartition is built by starting to add the list elements after the function first evaluates to True until the function evaluates to False on some successive element. Then the second subpartition is searched in the same way, starting from this last element and the program continues until the end of the list.

PartitionWhile accepts the following option:

  • shortest (= True): change the length of the partitions

Different partitions, always satisfying the given condition, can be determined through different values for this option:

  • True : split always at the Shortest partition
  • False : search always the Longest partition
  • k : search k-1 next elements after the shortest partition

For example:

print(PartitionWhile([-5,8,1,2,6,-20,8,9,-5,7,3], lambda x: sum(x) <= 10))
#[[-5, 8, 1, 2], [6, -20, 8, 9, -5, 10]]

print(PartitionWhile([-5,8,1,2,6,-20,8,9,-5,7,3], lambda x: sum(x) <= 10,shortest=False))
#[[-5, 8, 1, 2, 6, -20, 8, 9, -5], [7, 3]]

More details and examples are given in the documentation for the homonymous resource function on the Wolfram Function Repository.

Metadata

Release files for partition-while 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for partition-while 1.0.0
File Size Uploaded
partition_while-1.0.0.tar.gz 2.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for partition-while 1.0.0
File Interpreter ABI Platform
partition_while-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.1 kB

Release files / partition_while-1.0.0.tar.gz

Download URL partition_while-1.0.0.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6ddea7d2867f527e00be93636bce7f8b47f2e4d236465a0ceb37416b56131ac0
BLAKE2b-256 checksum
How to use checksums
1397f19d0fb354928dc9a27a500c03c2b9f7e26c31575b41204465d057bba1fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.3

Release files / partition_while-1.0.0-py3-none-any.whl

Download URL partition_while-1.0.0-py3-none-any.whl
Size 2.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
33cd12290a98ff8ba7f3e1cf6d2123628291346c0f3d4fd4e93d92599b9a88e1
BLAKE2b-256 checksum
How to use checksums
60d4c8bb0d6dfe33229401bb091e5826da03aff8ac6615438dfd77e5624a1556
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page