selecting a random item from a set

Alexander Schmolck a.schmolck at gmx.net
Tue Dec 30 18:13:20 EST 2003


Quite a few algortihms prominently feature choosing/removing a single random
element from a set at a time. On first sight I can't think of anything better
to achieve this with `sets.Set` than something along the lines of:

  for dummy, randomElement in zip(range(randrange(len(s)+1)), s): pass
  # possibly followed by
  s.remove(randomElement)

Is there a better way? If not, how about something like Set.randompop()?

'as




More information about the Python-list mailing list