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


Groups > comp.lang.python > #61585

Re: grab dict keys/values without iterating ?!

Date 2013-12-11 09:42 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: grab dict keys/values without iterating ?!
References (1 earlier) <52A7AB8C.8030700@arcor.de> <almarsoft.6523303589308130554@news.gmane.org> <mailman.3885.1386762407.18130.python-list@python.org> <3efc283f-419d-41b6-ad20-c2901c3b9f78@googlegroups.com> <roy-DA38A7.09461211122013@news.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.3903.1386776501.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2013-12-11 09:46, Roy Smith wrote:
> The problem is, that doesn't make a whole lot of sense in Python.
> The cited implementation uses dicts at each level.  By the time
> you've done that, you might as well just throw all the data into
> one big dict and use the full search string as the key.  It would
> be a lot less code, and probably run faster.

You're right if the search term is a whole word, a single
dict-to-result works ideally. However, the OP asked about prefixes, so
the Python implementation I provided uses a dict-of-nested-dicts which
allows any arbitrary prefix, and then iterates over only the subset of
those that match.

If you need O(length-of-prefix) iteration of all results, I believe
that's the best way algorithm to use (implementation details could
differ for a more space-efficient structure perhaps; normalization
might help reduce dict-entries).

It's a specialized use-case, and doesn't have the O(1) lookup for
exact-matches (that's just a special case of prefix=word with no
sub-iteration, so would be O(length-of-search-word)).	

-tkc


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


Thread

Re: grab dict keys/values without iterating ?! Tamer Higazi <tameritoke2@arcor.de> - 2013-12-11 12:07 +0200
  Re: grab dict keys/values without iterating ?! rusi <rustompmody@gmail.com> - 2013-12-11 05:31 -0800
    Re: grab dict keys/values without iterating ?! Roy Smith <roy@panix.com> - 2013-12-11 09:46 -0500
      Re: grab dict keys/values without iterating ?! rusi <rustompmody@gmail.com> - 2013-12-11 07:08 -0800
      Re: grab dict keys/values without iterating ?! Tim Chase <python.list@tim.thechases.com> - 2013-12-11 09:42 -0600
    Re: grab dict keys/values without iterating ?! Travis Griggs <travisgriggs@gmail.com> - 2013-12-11 09:19 -0800
    Re: grab dict keys/values without iterating ?! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-11 18:09 +0000
  Re: grab dict keys/values without iterating ?! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-11 13:44 +0000
    Re: grab dict keys/values without iterating ?! Tim Chase <python.list@tim.thechases.com> - 2013-12-11 08:30 -0600
    Re: grab dict keys/values without iterating ?! Ian Kelly <ian.g.kelly@gmail.com> - 2013-12-11 18:02 -0700
    Re: grab dict keys/values without iterating ?! Ian Kelly <ian.g.kelly@gmail.com> - 2013-12-11 18:05 -0700

csiph-web