Problem using Random
Peter Hansen
peter at engcorp.com
Sun Oct 28 01:05:34 EDT 2001
Jeremy Whetzel wrote:
>
> mylist = ['a','b','c','d','e','f']
> print mylist
> for i in mylist:
> c = random.choice(mylist)
> print c
> mylist.remove(c)
>
> Is there any way that I can get the random.choice() to go through and
> exhaust all possible choices before quitting?
There are many ways... :)
Your problem above is in iterating over the list with 'i'. Why
do that, when you are not even using 'i' in the code?
Try something like this instead, as a quick first attempt.
(Note that the conditional evaluation of mylist returns false
when the list is empty, exiting the loop.)
>>> import random
>>> mylist = ['a','b','c','d','e','f']
>>> while mylist:
... c = random.choice(mylist)
... print c
... mylist.remove(c)
f
a
d
b
c
e
--
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com
More information about the Python-list
mailing list