Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How do you sort a name list and list in random order?

Reply
Thread Tools

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

 
 
Frank & Janny Plaza
Guest
Posts: n/a
 
      08-24-2003
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

 
Reply With Quote
 
 
 
 
Heather Coppersmith
Guest
Posts: n/a
 
      08-24-2003
On Sat, 23 Aug 2003 20:03:42 -0700 (PDT),
Frank & Janny Plaza <(E-Mail Removed)> wrote:

> 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/li...le-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

--
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli

 
Reply With Quote
 
 
 
 
Carsten Schultz
Guest
Posts: n/a
 
      08-24-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.
 
Reply With Quote
 
Tobias Pfeiffer
Guest
Posts: n/a
 
      09-22-2003
Hi!

Frank & Janny Plaza <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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
 
Reply With Quote
 
Kenneth Hutson
Guest
Posts: n/a
 
      09-23-2003
What about shuffle()?

"Tobias Pfeiffer" <(E-Mail Removed)> wrote in message
news:bknkiq$3o6va$(E-Mail Removed)-berlin.de...
> Hi!
>
> Frank & Janny Plaza <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
> > 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute cjt22@bath.ac.uk Python 7 09-10-2007 11:10 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments