how can i randomly choose keys in dictionary
John Lenton
john at grulic.org.ar
Fri Feb 18 09:44:53 EST 2005
On Fri, Feb 18, 2005 at 09:35:30AM +0100, Thomas Guettler wrote:
> Am Thu, 17 Feb 2005 18:13:46 -0800 schrieb neutrinman:
>
> > Hi,there. How can I choose a key in dictionary randomly?
> >
> > Say, random.choice() in lists,
> >
> > or in lists:
> > lists = [1,2,3,4]
> > position = random.range(len(lists))
> > word = lists[position]
>
> Hi, try this:
>
> import random
>
> mydict={1: "one", 2: "two"}
> print mydict[random.choice(mydict.keys())]
if you're going to do that, why don't you do
print random.choice(mydict.values())
?
--
John Lenton (john at grulic.org.ar) -- Random fortune:
Quien da parte de sus cohechos, de sus tuertos hace derechos.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20050218/d1de7392/attachment.sig>
More information about the Python-list
mailing list