Re: select random entry from dictionary

2005-03-10 Thread Skip Montanaro
>> d.popitem() Removes and returns an arbitrary (key, value) pair from d >> If this isn't random enough, then you can generate a random number in >> range(len(d)) Peter> Although this idea may suit the OP, "arbitrary" is most Peter> definitely not "random". Correct. The libr

Re: select random entry from dictionary

2005-03-08 Thread [EMAIL PROTECTED]
Peter, Agreed... which is why I said the 'random' module should be imported if more randomness is required. I only mentioned d.popitem() first in case Tores' application didn't need a psuedo-random item and instead he was looking to pull any value without randomness. -- http://mail.python.org/m

Re: select random entry from dictionary

2005-03-08 Thread Peter Hansen
[EMAIL PROTECTED] wrote: From the Python 2.4 quickreference: d.popitem() Removes and returns an arbitrary (key, value) pair from d If this isn't random enough, then you can generate a random number in range(len(d)) Although this idea may suit the OP, "arbitrary" is most definitely not "ran

Re: select random entry from dictionary

2005-03-07 Thread Scott David Daniels
Tony Meyer wrote: How can I select a random entry from a dictionary, regardless of its key-values? a = random.choice(d.keys()) a, d[a] Or even: key, value = random.choice(d.items()) or: value = random.choice(d.values()) --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailm

Re: select random entry from dictionary

2005-03-07 Thread chengdu
import random print d[d.keys()[int(random.random()*len(d.keys()))]] HTH -c -- http://mail.python.org/mailman/listinfo/python-list

Re: select random entry from dictionary

2005-03-07 Thread [EMAIL PROTECTED]
>From the Python 2.4 quickreference: d.popitem() Removes and returns an arbitrary (key, value) pair from d If this isn't random enough, then you can generate a random number in range(len(d)) -- http://mail.python.org/mailman/listinfo/python-list

RE: select random entry from dictionary

2005-03-07 Thread Tony Meyer
> How can I select a random entry from a dictionary, regardless of its > key-values? >>> import random >>> d = {1:'a', 2:'b', 3:'c'} >>> a = random.choice(d.keys()) >>> a, d[a] (2, 'b') (etc) =Tony.Meyer -- http://mail.python.org/mailman/listinfo/python-list