Unsorting(randomizing) a sequence

Dan Schmidt dfan at harmonixmusic.com
Tue Aug 17 15:25:57 EDT 1999


Jeremy Hylton <jeremy at cnri.reston.va.us> writes:

| I've rather fond of the following approach:
| 
| import random
| 
| def randomize(a, b):
|     return random.choice([-1, 0, 1])
| 
| l = range(2, 20)
| l.sort(randomize)

I'm not sure how robust this is.  I've seen at least one implementation
of qsort crash when given an inconsistent comparison function (e.g.,
one that doesn't obey transitivity).  And how is sort() supposed to
know when it's done?

-- 
                 Dan Schmidt -> dfan at harmonixmusic.com, dfan at alum.mit.edu
Honest Bob & the                http://www2.thecia.net/users/dfan/
Factory-to-Dealer Incentives -> http://www2.thecia.net/users/dfan/hbob/
          Gamelan Galak Tika -> http://web.mit.edu/galak-tika/www/




More information about the Python-list mailing list