New submission from Lex Flagel <[email protected]>:
It would be nice to make random.sample and random.choice both have the same
behavior with iterators. Currently random.sample accepts them happily, and
whereas random.choice does not. E.g.
> import random
> d = {'a':1, 'b':2}
> random.sample(d.keys(),1)
Out: ['a']
> random.choice(d.keys())
Out: TypeError: 'dict_keys' object is not subscriptable
random.choice could be redefined as follows to harmonize behavior, but I think
the solution for random.choices maybe be more involved:
def choice(x):
random.sample(x,1)[0]
----------
components: Library (Lib)
messages: 348680
nosy: Lex Flagel
priority: normal
severity: normal
status: open
title: Harmonize random.choice(s) behavior with random.sample on iterators
type: enhancement
versions: Python 3.8
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue37708>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com