faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes

Entry

Unsorting (randomizing) a sequence

Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 243, Jeremy Hylton


"""
Packages: basic_datatypes.lists;maths.random
"""
"""
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)
print l
[2, 3, 4, 5, 19, 6, 17, 13, 16, 7, 8, 18, 10, 14, 9, 12, 11, 15]
"""
No idea how fast this is, though not very I expect.
Jeremy
"""