Skip to main content

Partition a list by a condition on the sublists

Project description

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.

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-1.0.0.tar.gz (2.4 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-1.0.0-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for partition_while-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6ddea7d2867f527e00be93636bce7f8b47f2e4d236465a0ceb37416b56131ac0
MD5 96359e88a54d8d06df5552e3e94fe91f
BLAKE2b-256 1397f19d0fb354928dc9a27a500c03c2b9f7e26c31575b41204465d057bba1fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for partition_while-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 33cd12290a98ff8ba7f3e1cf6d2123628291346c0f3d4fd4e93d92599b9a88e1
MD5 98601161f349d7696da4fa3a823b9522
BLAKE2b-256 60d4c8bb0d6dfe33229401bb091e5826da03aff8ac6615438dfd77e5624a1556

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