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


Groups > comp.lang.python > #61635

Re: grab dict keys/values without iterating ?!

References <52A7AB8C.8030700@arcor.de> <almarsoft.6523303589308130554@news.gmane.org> <mailman.3885.1386762407.18130.python-list@python.org> <52a86c57$0$29992$c3e8da3$5496439d@news.astraweb.com> <20131211083051.65e3d501@bigbox.christie.dr>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-12-11 18:02 -0700
Subject Re: grab dict keys/values without iterating ?!
Newsgroups comp.lang.python
Message-ID <mailman.3944.1386810211.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Dec 11, 2013 at 7:30 AM, Tim Chase
<python.list@tim.thechases.com> wrote:
> On 2013-12-11 13:44, Steven D'Aprano wrote:
>> If necessary, I would consider having 26 dicts, one for each
>> initial letter:
>>
>> data = {}
>> for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
>>     data[c] = {}
>>
>> then store keys in the particular dict. That way, if I wanted keys
>> starting with Aa, I would only search the A dict, not the B dict, C
>> dict, etc.
>
> That's what the convoluted code does that I put at the end of my
> previous post in this thread, only to the Nth degree (the outermost
> dict has the first letter which links to a dictionary of the 2nd
> level/letter, to the 3rd level/letter, etc).

This is what I did not so long ago when writing a utility for
typeahead lookup, except that to save some space and time I only
nested the dicts as deeply as there were still multiple entries.  As
an example of what the data structure looked like:

lookups = {
    'a': {
        'l': {
            'g': 'algebra',
            'p': 'alphanumeric',
        },
        's': 'asterisk',
    },
    'b': 'bobcat',
    ...
}

It does make the update process more complicated though, as adding new
words potentially requires existing words to be nested deeper than
they are currently.

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