Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61558

Re: grab dict keys/values without iterating ?!

From Peter Otten <__peter__@web.de>
Subject Re: grab dict keys/values without iterating ?!
Date 2013-12-11 13:10 +0100
Organization None
References <52A7AB8C.8030700@arcor.de> <52A7AB8C.8030700@arcor.de> <almarsoft.6523303589308130554@news.gmane.org> <52A8394C.5070402@arcor.de>
Newsgroups comp.lang.python
Message-ID <mailman.3891.1386763843.18130.python-list@python.org> (permalink)

Show all headers | View raw


Tamer Higazi wrote:

> Hi Dave!
> 
> You were absolutely right.
> I don't want to iterate the entire dict to get me the key/values
> 
> Let us say this dict would have 20.000 entries, but I want only those
> with "Aa" to be grabed.
> Those starting with these 2 letters would be only 5 or 6 then it would
> take a lot of time.
> 
> In which way would you prefer to store the data, and which functions or
> methods would you use effectively to accomplish this task ?

Well, Dave already gave one approach:

[Dave Angel]
> For example if you stored all the keys in a sorted list you could use 
> bisect.

See also http://docs.python.org/dev/library/bisect.html

Another option would be to use a database. Assuming the table 'lookup' has 
two columns 'key' and 'value' you'd get the matching rows with

select key, value from lookup where key like 'Aa%';

A lightweight database that comes with Python is sqlite:

http://docs.python.org/dev/library/sqlite3.html
http://www.sqlite.org/

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: grab dict keys/values without iterating ?! Peter Otten <__peter__@web.de> - 2013-12-11 13:10 +0100

csiph-web