[poll] anyone else needs fast random element access off a dictionary?

Michal Vitecek fuf at mageo.cz
Tue Feb 11 11:37:25 EST 2003


 hello everyone,

 i'd like to ask if there exist people (like me) who have a need to get
 random elements off a dictionary and cannot use lists because they need
 a fast data lookup/retrieval.

 for example i'm in need of a _fast_ random element access because i use
 dictionary a) as a mains to have distinct data and b) use the distinct
 data to make a more diversive population in my memetic algorithm (the
 more diversive the population is, the better behaviour of the
 algorithm).

 currently one can get a random key stored in a dictionary object like
 this:

    random.choice(dict.keys())

 the same goes for a random value stored in a dictionary:

    random.choice(dict.values())

 both those methods have one big drawback - they create a list of either
 keys or values each time a random element is needed which means a very
 noticeable slowdown. [as a sidenote, random.choice() doesn't work with
 dictionaries.]

 i wrote a patch for python 2.3, but it was rejected because it's
 supposedly too specific so i'm in search for other people who have a
 need for a fast random element access stored in a builtin dictionary.

    thank you,
-- 
		fuf		(fuf at mageo.cz)





More information about the Python-list mailing list