Random from Dictionary

Chris Barker chrishbarker at home.net
Thu Oct 18 12:54:54 EDT 2001


Mikael Olofsson wrote:

> Couldn't you use it on yourdict.keys()?

you sure could:

>>> from random import choice
>>> d = {"a":1, "b":2, "c":3}
>>> d
{'b': 2, 'c': 3, 'a': 1}
>>> d[choice(d.keys())]
3
>>> d[choice(d.keys())]
1
>>> d[choice(d.keys())]
3

tested and everything....

-Chris



-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list