dictionnaries and lookup tables

m.barenco at gmail.com m.barenco at gmail.com
Tue Oct 11 14:06:32 EDT 2005


Hello,

I am considering using dictionnaries as lookup tables e.g.

>>>D={0.5:3.9,1.5:4.2,6.5:3}

and I would like to have a dictionnary method returning the key and
item of the dictionnary whose key is smaller than the input of the
method (or <=,>,>=) but maximal (resp. maximal,minimal,minimal) eg.:

>>>D.smaller(3.0)
(1.5,4.2)
>>>D.smaller(11.0)
(6.5,3)
>>>D.smaller(-1.0)
None (or some error message)

Now, I know that dictionnaries are stored in a non-ordered fashion in
python but they are so efficient in recovering values (at least wrt
lists) that it suggests me that internally there is some ordering. I
might be totally wrong because I don't know how the hashing is really
done. Of course I would use such methods in much larger tables. So is
this possible or should I stick to my own class with O(log2(N))
recovery time?

Note that when I type:
>>>dir(D)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__',
'__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__',
'__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__',
'__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__setitem__', '__str__', 'clear', 'copy',
'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys',
'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update',
'values']
the functions __ge__, __gt__, __lt__, __le__ seem to be non-implemented
but there is some __doc__ in them. Is there the intention to do
something similar as is described above or are they here for some
(future) dictionnary comparison purposes?

Thanks a lot!

Martino




More information about the Python-list mailing list