This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients rhettinger, tim.peters
Date 2020-05-06.22:35:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1588804520.3.0.249071627671.issue40541@roundup.psfhosted.org>
In-reply-to
Content
I've seen multiple requests for this and it isn't obvious how to do it with the existing tooling.  

The example currently given in the recipes section isn't scalable because it involves expanding the population into a big list with repeated elements:

      sample(['x', 'x', 'x', 'x', 'y', 'y', 'z'], k=5)

Example task:  Given an urn with 8 red balls, 2 white balls, and 3 green balls, choose ten without replacement:

    >>> population = ['red', 'blue', 'green']
    >>> weights =    [  8,      5,      3   ]
    >>> sample(population, weights=weights, k=10)
    ['red', 'green', 'blue', 'red', 'green', 'blue', 'red', 'blue', 'red', 'blue']

I could also add *cum_weights* as an optional optimization but think it best to wait until someone asks for it ;-)
History
Date User Action Args
2020-05-06 22:35:20rhettingersetrecipients: + rhettinger, tim.peters
2020-05-06 22:35:20rhettingersetmessageid: <1588804520.3.0.249071627671.issue40541@roundup.psfhosted.org>
2020-05-06 22:35:20rhettingerlinkissue40541 messages
2020-05-06 22:35:19rhettingercreate