[Python-ideas] Allow random.choice, random.sample to work on iterators

Chris Kaynor ckaynor at zindagigames.com
Wed Nov 30 14:52:51 EST 2016


This was also brought up back in April:
https://mail.python.org/pipermail//python-ideas/2016-April/039707.html

It got a few replies from Guido
(https://mail.python.org/pipermail//python-ideas/2016-April/039713.html
for one of them).

It seems the idea got dropped due to problems with making it properly
random (practically, the sampling has issues in cases of very large
sequences, let along infinite) and performance (either large numbers
of calls to random, or copying the sequence, each of which has its own
problems).

There are also issues with how it should behave on iterables that
cannot be re-iterated (eg, random.choice will consume the iterator,
and could only be called once safely).
Chris


On Wed, Nov 30, 2016 at 11:42 AM, Bernardo Sulzbach
<mafagafogigante at gmail.com> wrote:
> On 2016-11-30 17:25, Random832 wrote:
>>
>> Currently these functions fail if the supplied object has no len().
>> There are algorithms for this task that can work on any finite iterator
>> (for example, files as a stream of lines), and the functions could fall
>> back to these if there is no len().
>
>
> I like the idea, as long as it does not add too much overhead to currently
> existing code.
>
> It could be a special code path for reservoir sampling (I assume) for both
> functions (the first taking only one sample from the stream).
>
>
> --
> Bernardo Sulzbach
> http://www.mafagafogigante.org/
> mafagafogigante at gmail.com
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list