Skip to main content

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

Metadata

Release files for ratio-merge 0.3.2

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

Source distribution (sdist)

Source distribution for ratio-merge 0.3.2
File Size Uploaded
ratio_merge-0.3.2.tar.gz 1.9 kB Details

Release files / ratio_merge-0.3.2.tar.gz

Download URL ratio_merge-0.3.2.tar.gz
Size 1.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d8211a9458b606130782d57fed541c520c82dc0e14221b8360e4181ab81fdb28
BLAKE2b-256 checksum
How to use checksums
bf2c49aad686bdfc51179ab59fa011548e250cfc0644146bc90cd42318fdfb27
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.3.2 This release

1 release file

0.3.1

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

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