Random from Dictionary

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Oct 18 13:09:44 EDT 2001


"Emile van Sebille" <emile at fenx.com> writes:

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

Not for me:

Python 2.1.1 (#1, Jul 21 2001, 20:59:12) 
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.
>>> x={'Hallo':'Welt'}
>>> import random
>>> random.choice(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.1/random.py", line 329, in choice
    return seq[int(self.random() * len(seq))]
KeyError: 0

Perhaps you where using integer keys?

Regards,
Martin



More information about the Python-list mailing list