R: Sorted list as an alternative to dictionary for when you only needkeys?

Daniele Varrazzo dvarrazzo at virgilio.it
Mon Jul 19 16:46:45 EDT 2004


If you need a container to look into, there is the sets module that provides
a couple of them.

If you need a sorted list, there is the bisect module. But i don't think it
fits your need for a quick lookup: it's a python module so probably it's
slower. Furthermore you can __hash__ almost everything, but you can __cmp__
in a consistent way much less stuff.

Take definitely a look at sets.

-----Messaggio originale-----
Da: python-list-bounces+dvarrazzo=virgilio.it at python.org
[mailto:python-list-bounces+dvarrazzo=virgilio.it at python.org]Per conto di
Daniel Eloff
Inviato: lunedi 19 luglio 2004 22.19
A: python-list at python.org
Oggetto: Sorted list as an alternative to dictionary for when you only
needkeys?


I've used a dictionary with None values before when I've needed a container
with fast lookup by value. Just insert all the values as keys.

But this is not optimal at all. Better would be a python list that is sorted
and uses a binary search algo to find values. Now it's not that much work to
write a wrapper class for list that does this, but is there maybe not such a
thing already in Python (surely it's very common, it's like a C++ STL set or
a .NET sorted list, or a.). Or at least a wrapper already written in Python.

I think this really should be a data structure added to the language, if it
isn't already.

-Dan




More information about the Python-list mailing list