How do you sort a name list and list in random order?

Carsten Schultz carsten at gnocchi.dialup.fu-berlin.de
Sun Aug 24 10:36:05 EDT 2003


Frank & Janny Plaza:
> I am trying to write a program in which the users will each enter
> their names and when all names have been entered, I want to randomly
> sort this list of names several times
[...]
> Thanks, Cisco

Hi Frank, Janny or Cisco!

My first idea was the following:

==============
import random

def shuffle(l):
    randomly_tagged_list = [(random.random(), x) for x in l]
    randomly_tagged_list.sort()
    return [x for (r, x) in randomly_tagged_list]


t=["Anna", "Berta", "Caesar", "Doris"]

print shuffle(t)
print shuffle(t)
print t
==============

You may want to also have a look at random.shuffle, however.

Greetings,

Carsten

-- 
Carsten Schultz (2:40, 33:47), FB Mathematik, FU Berlin
http://carsten.fu-mathe-team.de/
PGP/GPG key on the pgp.net key servers, 
fingerprint on my home page.




More information about the Python-list mailing list