Reading a random element from a set
ast
nomail at com.invalid
Wed Jul 26 03:49:22 EDT 2017
More information about the Python-list mailing list
Wed Jul 26 03:49:22 EDT 2017
- Previous message (by thread): Reading a random element from a set
- Next message (by thread): Reading a random element from a set
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Steven D'Aprano" <steve+comp.lang.python at pearwood.info> a écrit dans le message de news:597841eb$0$2878$c3e8da3$76491128 at news.astraweb.com... > On Wed, 26 Jul 2017 08:58:03 +0200, ast wrote: > >> Hello >> >> random.choice on a set doesn't work because sets are not indexable >> >> so I found nothing better than taking an element and puting it back >> >> a = {5, 7, 8, 3, 0, 8, 1, 15, 16, 34, 765443} >> elt = a.pop() >> a.add(elt) > > > That's not *random*, it is arbitrary but predictable. If you try it twice > in a row, you will probably get the same element each time. (Depends on > the elements, but very likely.) > > If you need a random element, best way is: > > > >>>> random.choice(list(a)) > 8 >>>> random.choice(list(a)) > 15 >>>> random.choice(list(a)) > 16 > Thanks random.sample(a, 1)[0] works too
- Previous message (by thread): Reading a random element from a set
- Next message (by thread): Reading a random element from a set
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list