Skip to main content

Partition a list by a condition on the sublists

Project description

partition-while

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

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 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
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]]

Project details


Download files

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

Source Distribution

partition_while-0.1.1.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

partition_while-0.1.1-py3-none-any.whl (2.5 kB view details)

Uploaded Python 3

File details

Details for the file partition_while-0.1.1.tar.gz.

File metadata

  • Download URL: partition_while-0.1.1.tar.gz
  • Upload date:
  • Size: 2.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.3

File hashes

Hashes for partition_while-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2fb4c63054dcd562489b600f823a5fe8515176621161b3ec0c07127f56f7fa82
MD5 3e581b73e459b56c78d07c3a177c8ab1
BLAKE2b-256 b558327717b33a195d2a4ea39981da496c2cae45b32bbaeca3368b750f6e892d

See more details on using hashes here.

File details

Details for the file partition_while-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for partition_while-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4e8110566d78b303a67081a09e8f1de90e8c8e3df0402d4f48a5f991cb6d6507
MD5 b8a832b15172ae0d57e701a3889c0f67
BLAKE2b-256 e63c0f45442eeb2454f6ed44bafada5c3cfab3572628efda53359c352cd4b856

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page