
July 10, 2020
9:42 p.m.
I'm -1 too. Making ordered containers to sequence-like only for random.choice is a wrong idea. If random.choice should support non-sequence ordered container, just propose it to random.choice. And if the proposal was rejected, you can do it by yourself with helper functions.
import random from itertools import islice def nth(iterable, n): ... return next(islice(iterable, n, None)) def choice_from_container(c): ... return nth(c, random.randrange(len(c))) d = dict.fromkeys(range(10000)) choice_from_container(d) 61 choice_from_container(d) 9858 choice_from_container(d.keys()) 2436 choice_from_container(d.items()) (7685, None)
-- Inada Naoki <songofacandy@gmail.com>