dictionnaries and lookup tables

2005-10-11 Thread m . barenco
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionnaries and lookup tables

2005-10-11 Thread m . barenco
>Sure they're implemented.

Oops, my apologies.

Just to build up on that, when I run:

#start of listing
import random

A={1:None,2:None,"hello":None,(1,2,3):None}

def dictcomp(n):
for i in range(n):
B=A.copy()
C=A.copy()
b=random.uniform(0,1)
c=random.uniform(0,1)
B[b]=None
C[c]=None
res=((B>C)==(b>c))
print res,

dictcomp(1000)
#end of listing

I get 1000 True's on the output, which suggests that key-wise ordering
is implemented in some guise. The question is: how do I access that?

Thanks

Martino

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionnaries and lookup tables

2005-10-11 Thread m . barenco
Ok, Thanks for your answers, that's pretty unambiguous.

M.

-- 
http://mail.python.org/mailman/listinfo/python-list