random.choices() Suggest that the code confirm that cum_weights sequence is in ascending order
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon May 14 07:36:04 EDT 2018
Hi Paul, and welcome!
On Sun, 13 May 2018 17:48:47 -0700, Paul wrote:
> Hi,
> I just learned how to use random.choices().
[...]
> Consequently, I specified 'cum_weights' with a sequence which wasn't in
> ascending order. I got back k results but I determined that they
> weren't correct (eg, certain population values were never returned).
>
> Since the non-ascending sequence, which I had supplied, could not
> possibly be valid input, why isn't this checked (and an error returned)?
> Returning incorrect results (which could be hard to spot as being
> incorrect) is much more dangerous. Also, checking that the list is in
> ascending order need only be done once, and seems like it would be
> inexpensive.
Sounds like a reasonable feature request to me.
https://bugs.python.org/issue33494
--
Steve
More information about the Python-list
mailing list