dictionnaries and lookup tables

jepler at unpythonic.net jepler at unpythonic.net
Tue Oct 11 14:17:31 EDT 2005


On Tue, Oct 11, 2005 at 11:06:32AM -0700, m.barenco at gmail.com wrote:
> Note that when I type:
> >>>dir(D)
[...]
> 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?

Sure they're implemented.  That's why I can compare dictionaries for equality
or order.

>>> d = {1: None}
>>> e = {1: None}
>>> f = {2: None}
>>> d == e
True
>>> d == f
False
>>> d < f or f < e
True

If the operation you need to optimize is "find the largest element that is
smaller than X", then perhaps you want to use the bisect module.  This involves
keeping your information in a sorted list of tuples, instead of in a
dictionary.  However, removing or inserting items has O(n) complexity instead
of O(1).

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20051011/37528e58/attachment.sig>


More information about the Python-list mailing list