473,466 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

Jul 18 '05 #1
4 13221
On Sat, 23 Aug 2003 20:03:42 -0700 (PDT),
Frank & Janny Plaza <pl******@sbcglobal.net> 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

Jul 18 '05 #2
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.
Jul 18 '05 #3
Hi!

Frank & Janny Plaza <pl******@sbcglobal.net> wrote in
news:ma**********************************@python.o rg:
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
Jul 18 '05 #4
What about shuffle()?

"Tobias Pfeiffer" <Bo**************@web.de> wrote in message
news:bk************@ID-162581.news.uni-berlin.de...
Hi!

Frank & Janny Plaza <pl******@sbcglobal.net> wrote in
news:ma**********************************@python.o rg:
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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jefferis NoSpamme | last post by:
I have a query that is showing new items in a store based upon their inventory date. The problem with my Random order sort is that when the "next" link is pressed, the random function is called...
22
by: Nhmiller | last post by:
Is there a way to do this? Thanks. Neil Cat Paintings At Carol Wilson Gallery http://www.carolwilsongallery.com
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
2
by: Paul Evans | last post by:
Hi, I've created a datagrid which works fine accept the id field (the primary key field) comes out in a random order. How do I get the fields to display sequencially? Thanks for your time ...
9
by: Tuxedo | last post by:
I'd like to reorganize the third, fourth, fifth and sixth, as well as any elements thereafter in an array in random order: var a = new...
2
by: federicog | last post by:
I have to create a listing in random order from a few mysql tables. I can't use ORDER BY RAND() for a few reasons: - The random order has to change only every 24hs, ie: today it will show rows...
2
by: dniom | last post by:
Hi. Can you help me please? Is there any way to show a unique random item from an array, every time you refresh the page? In other words how do I show the items from an array in random order, one by...
0
by: Joe Cagg | last post by:
I have looked All over the Internet to try and find an example of How to fix the following. How to Sort a List in Desending Order by the "Value" and Sort the Duplicates by the "Key" ? Then Print...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.