Skip to main content

A small utility function for merging two lists by some ratio, keeping the merged list constantly as close as possible to the target ratio

Project description

A small utility function for merging two python lists by some ratio

This function acts as a generator producing elements from the two given lists by the given <merge_ratio>. The generator would preserve any internal order within the lists and would produce no more then <desired_length> elements in total.

In case the total number of elements within either list is not enough to maintain the desired ratio throughout, the function would continue to generate elements from the remaining set until <desired_length> elements were produced.

If the <strict> parameter is set to True the function will stop producing elements once one of the lists has ran out of elements. It would also bias the produced list to fit closest to the length of the <first_list>.

Install

pip install ratio_merge

Usage Examples:

~$ python
>>> from ratio_merge import ratio_merge
>>>
>>> x=[1,2,3,4,5]
>>> y=['a','b','c','d','e','f','g']
>>>
>>> l = ratio_merge(x,y, 0.7) # <- evaluates on the fly
>>> l
    <generator object ratio_merge at 0x7fd93902d870>
>>> list(l)
    [1, 'a', 2, 3, 'b', 4, 5, 'c', 'd', 'e']
>>>
>>> l = ratio_merge(x,y, 0.7, 5) # <- length limit
>>> list(l)
    [1, 'a', 2, 3, 'b']
>>>
>>> l = ratio_merge(x,y, 0.2) # <- differant ratio
>>> list(l)
    [1, 'a', 'b', 'c', 'd', 2, 'e', 3, 4, 5]
>>>
>>>
>>> l = ratio_merge(x,y, 0.5) # <- replicates zip order on 0.5
>>> list(l)
    [1, 'a', 2, 'b', 3, 'c', 4, 'd', 5, 'e']
>>> zip(x,y)
    [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]

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

ratio_merge-0.3.2.tar.gz (1.9 kB view details)

Uploaded Source

File details

Details for the file ratio_merge-0.3.2.tar.gz.

File metadata

  • Download URL: ratio_merge-0.3.2.tar.gz
  • Upload date:
  • Size: 1.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.9

File hashes

Hashes for ratio_merge-0.3.2.tar.gz
Algorithm Hash digest
SHA256 d8211a9458b606130782d57fed541c520c82dc0e14221b8360e4181ab81fdb28
MD5 4fd9341a1b423499e5ca988616134a78
BLAKE2b-256 bf2c49aad686bdfc51179ab59fa011548e250cfc0644146bc90cd42318fdfb27

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