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

  • Thread starter Frank & Janny Plaza
  • Start date
F

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 and then show the order in which they have beeen sorted. I tried using sort(), but this only sorts the names in the order in which they are entered. Any suggestions?
Thanks, Cisco
 
H

Heather Coppersmith

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 and then show
the order in which they have beeen sorted. I tried using sort(),
but this only sorts the names in the order in which they are
entered. Any suggestions?

I'm not quite sure what "sorts the names in the order in which
they are entered" means, but take a look at the shuffle function
in the random module:

http://www.python.org/doc/current/lib/module-random.html
import random
x = range( 10 )
x [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
random.shuffle( x )
x [5, 8, 3, 0, 1, 7, 6, 9, 2, 4]
random.shuffle( x )
x [5, 2, 4, 1, 3, 9, 6, 0, 7, 8]
random.shuffle( x )
x
[2, 8, 7, 5, 0, 4, 3, 9, 6, 1]

HTH,
Heather
 
C

Carsten Schultz

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
 
T

Tobias Pfeiffer

Hi!

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

"randomly sort"??????? I mean, for me "randomly" means somewhat of the
opposite of "sorted"...

Bye
Tobias
 
K

Kenneth Hutson

What about shuffle()?

Tobias Pfeiffer said:
Hi!



"randomly sort"??????? I mean, for me "randomly" means somewhat of the
opposite of "sorted"...

Bye
Tobias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top