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

Discussion in 'Python' started by Frank & Janny Plaza, Aug 24, 2003.

  1. 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
    Frank & Janny Plaza, Aug 24, 2003
    #1
    1. Advertising

  2. On Sat, 23 Aug 2003 20:03:42 -0700 (PDT),
    Frank & Janny Plaza <> 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/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

    --
    Heather Coppersmith
    That's not right; that's not even wrong. -- Wolfgang Pauli
    Heather Coppersmith, Aug 24, 2003
    #2
    1. Advertising

  3. 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.
    Carsten Schultz, Aug 24, 2003
    #3
  4. Hi!

    Frank & Janny Plaza <> wrote in
    news::

    > 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
    Tobias Pfeiffer, Sep 22, 2003
    #4
  5. What about shuffle()?

    "Tobias Pfeiffer" <> wrote in message
    news:bknkiq$3o6va$-berlin.de...
    > Hi!
    >
    > Frank & Janny Plaza <> wrote in
    > news::
    >
    > > 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
    Kenneth Hutson, Sep 23, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    0
    Views:
    534
    nobody
    Jun 1, 2004
  2. =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=

    [B,IX] = sort(A,...) - Order for sort()-function

    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=, May 29, 2007, in forum: Python
    Replies:
    0
    Views:
    319
    =?iso-8859-1?Q?=22Orlando_D=F6hring=22?=
    May 29, 2007
  3. globalrev
    Replies:
    4
    Views:
    753
    Gabriel Genellina
    Apr 20, 2008
  4. Navin
    Replies:
    1
    Views:
    684
    Ken Schaefer
    Sep 9, 2003
  5. VK
    Replies:
    15
    Views:
    1,154
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page