dictionary sub-value lookup

Remco Gerlich scarblac at pino.selwerd.nl
Wed Feb 7 18:47:32 CET 2001


Richard <richard at millinfo.co.za> wrote in comp.lang.python:
> I am kinda new to this whole object orientation thing, so
> there is probably an easy way of doing this, but the
> tutorials etc are sparse.
> 
> I have an object M which contains N dictionaries. Inside
> dictionary N, is an identifier, Y, plus various other
> values, eg: Z.... is it possible to find M.Y.Z without
> looping through the whole of M, checking Y each time ?
> 
> Since I will have to do a LOT of this, and M may be large,
> I need a fast method.  

Dictionary lookups are fast, but it's rather unclear what you mean
exactly (what's an identifier in a dictionary, with various other values?)

If you have dictionaries of the type {"name1":"value1","name2":"value2" ,...},
and you're searching for a specific *value*, then you have to loop through
the hole thing. Dictionaries are lookup tables, they are one way (try
finding a name in the telephone book, given a phone number).

If you need this, it may be a good idea to keep a second dictionary with
the inverse relationship ({"value1":"name1",...}) but maybe you should first
try to explain yourself a bit better ;-).

-- 
Remco Gerlich



More information about the Python-list mailing list