Chris Angelico wrote:

> On Thu, Jan 18, 2018 at 6:28 AM, Ned Batchelder <n...@nedbatchelder.com>
> wrote:
>> You'll have to replace random.choice() with
>> random.choice(list(...)), since you can't random.choice from a set.
>>
> 
> Side point: why can't you? You can random.sample from a set, 

I'm not sure this was a good idea.

> but
> random.choice requires a sequence. It seems perfectly sane to ask for
> a random element from a set.

$ python3 -m timeit -s 'from random import sample; items = 
list(range(100000))' 'sample(items, 5)'
100000 loops, best of 3: 18 usec per loop
$ python3 -m timeit -s 'from random import sample; items = 
set(range(100000))' 'sample(items, 5)'
100 loops, best of 3: 7.27 msec per loop

You would need access to the set implementation to get reasonable 
performance.

As random.choice() is more likely to be called repeatedly with the same 
argument I'd rather not hide something like

if isinstance(seq, set):
    seq = tuple(set)

in its function body.


-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to