Random from Dictionary

Emile van Sebille emile at fenx.com
Thu Oct 18 10:16:12 EDT 2001


"Tim Payne" <tpayne at mac.com> wrote in message
news:181020010938443703%tpayne at mac.com...
> In article <9qml3o$ovl8k$1 at ID-11957.news.dfncis.de>, Emile van Sebille
> <emile at fenx.com> wrote:
>
> > Maybe choice in random helps out.
>
> I've always gotten this error when trying choice. I figured you just
> couldn't use it on a dictionary, but maybe something else is wrong with
> my install?
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python2.1/random.py", line 329, in choice
>     return seq[int(self.random() * len(seq))]
> KeyError: 1

I can't confirm that error.  I tested with 1.5.2, 2.0, 2.1 and 2.2a4+ and it
appears to work

If (for whatever reason) you don't get this resolved, you could do something
like:

kys = dict.keys()

for i in xrange(desired_count):
    dowhateverwith(dict[choice(kys)])

HTH,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list