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

Stuart Bishop zen at shangri-la.dropbear.id.au
Wed Feb 12 01:25:20 EST 2003


I have no need personally for such a beast, but...

On Wednesday, February 12, 2003, at 03:37  AM, Michal Vitecek wrote:

>  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).

One option:
	Use a dictionary to map key -> index, a list to store (key,value)
     tuples, and a wrapper to keep them in sync.
	You can then quickly pick a random element by pulling a random element
     out of the list (which also gives you the key to keep the index in 
sync).
     And the dictionary gives you fast lookup.

Using this method, you could get something more generically useful (a 
cross
between a dictionary and a list, at the cost of memory overhead) that 
may
have more of a chance of being included in Python.
-- 
Stuart Bishop <zen at shangri-la.dropbear.id.au>
http://shangri-la.dropbear.id.au/






More information about the Python-list mailing list