473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

dictionnaries and lookup tables

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

Oct 11 '05 #1
6 1492
On Tue, Oct 11, 2005 at 11:06:32AM -0700, m.*******@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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDTAG7Jd01MZaTXX0RApT8AKCE3d40OIZXFNQXTWqm9a 8xhJheVgCbBy/e
vm5m5gkTi+YViTmdNqkAdRM=
=z2yT
-----END PGP SIGNATURE-----

Oct 11 '05 #2
gry

m.*******@gmail.com wrote:
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?

....

I believe that to do this efficiently, you want some kind of tree, e.g.
B-tree, RB-tree, AVL-tree. You could try the AVL tree from:
ftp://squirl.nightmare.com/pub/pytho...avl-2.0.tar.gz

Oct 11 '05 #3
>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

Oct 11 '05 #4
m.*******@gmail.com wrote:
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?

You don't. There is no ordering of the keys, so there is no way that you
can implement the function you want.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 11 '05 #5
On Tue, 11 Oct 2005 11:50:24 -0700, m.barenco wrote:
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?


I don't think your code is showing what you think it is showing. I think
you have discovered an accidental invariant that depends on the *specific*
details of your code. In fact, I predict that if you run that code again,
you will randomly get 1000 Trues, or 1000 Falses, but never mixed Trues
and Falses.

Does this quote from the Python reference manual help you?

"Keys and values are listed in an arbitrary order which is non-random,
varies across Python implementations, and depends on the dictionary's
history of insertions and deletions."

http://docs.python.org/lib/typesmapping.html

What about this?

"Mappings (dictionaries) compare equal if and only if their sorted (key,
value) lists compare equal. Outcomes other than equality are resolved
consistently, but are not otherwise defined."

http://docs.python.org/ref/comparisons.html
In other words, even if you have discovered a consistent invariant
of dict behaviour, you can't rely on it -- it may disappear with the next
release of Python, or if you use an older version, or a version on a
different platform, or even because you've deleted an item from a dict and
then put it back in. (And if you know anything about typical
implementations of hash tables, you will understand why that last one is
quite reasonable.)


--
Steven.

Oct 11 '05 #6
Ok, Thanks for your answers, that's pretty unambiguous.

M.

Oct 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: DKode | last post by:
Ok, My staff has determined that we will be using custom business objects for a new app we are creating. we have an object that is called Employee. The employee object has about 8 lookup...
2
by: CoreyWhite | last post by:
The future of computer architecture will use lookup tables. Currently computer processor speed outweighs the benefits of using computer memory for lookup tables, except in some cases. As computer...
9
by: Koen | last post by:
Hi all, My application uses a lot of lookup tables. I've splitted the frontend (forms, reports, etc) from the backend (data). The database has around 10 different users. The values in the...
3
by: my-wings | last post by:
I've been reading about how evil Lookup fields in tables are, but I've got to be missing something really basic. I know this subject has been covered before, because I've just spent an hour or two...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
10
by: junky_fellow | last post by:
what are lookup tables ? How can they be used to optimise the code ?
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
3
by: dbuchanan | last post by:
Hello, (Windows forms - SQL Server) I fill my datagrid with a stored procedure that includes relationships to lookup tables so that users can see the values of the combobox selections rather...
1
by: paulquinlan100 | last post by:
Hi Im having problems getting a column in one of my tables to display the lookup values correctly. The database is split, in the backend the rowsource for this particular field is set to a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.