On Mon, Jul 13, 2020 at 7:44 AM Guido van Rossum
I think you all should get together and come up with a good implementation,
+1
and then petition Raymond Hettinger.
Is this Raymond's turf? I would think it belongs more in the random module than itertools, or is that Raymond's responsibility as well? In any case, a good implementation would be a lot easier to evaluate for inclusion. Or maybe there is an existing open source 3rd party project that has code
you can copy? I don’t recall if random has a C accelerator, but if it does, you should come up with C code as well.
Or Cython— at least to prototype it. -CHB
—Guido
On Mon, Jul 13, 2020 at 05:40 David Mertz
wrote: If we get this function (which I would like), the version with k items (default 1) is much better. Some iterators cannot be repeated at all, so not only is it slower to call multiple times if you need k>1, it's impossible.
On Mon, Jul 13, 2020, 8:37 AM David Mertz
wrote: This is an inefficient reservoir sampling. The optimized version does not need to call a random inclusion switch on every element, but can skip a geometrically ordered collection of (random) skip lengths to avoid most random inclusion decisions.
Obviously, all items must be iterated over no matter what, but if randrange() is significant compared to the cost of next(), the skip-then-decide version is much preferred, especially as size grows.
On Mon, Jul 13, 2020, 7:53 AM Oscar Benjamin
wrote: I posted this in the thread about indexing dict items but it seems to have been buried there so I'm starting a new thread.
Maybe there could be a function in the random module for making a random choice from an arbitrary (finite) iterable. This implementation can choose a random element from an iterable by fully iterating over it so is O(N) in terms of CPU operations but O(1) for memory usage:
[,,,]
--
--Guido (mobile) _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/DZ7KQZ... Code of Conduct: http://python.org/psf/codeofconduct/