[issue37708] Harmonize random.choice(s) behavior with random.sample on iterators
Lex Flagel
report at bugs.python.org
Mon Jul 29 15:28:24 EDT 2019
New submission from Lex Flagel <flag0010 at gmail.com>:
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 <report at bugs.python.org>
<https://bugs.python.org/issue37708>
_______________________________________
More information about the Python-bugs-list
mailing list