Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.045 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'nested': 0.07; 'etc).': 0.09; 'subject:keys': 0.09; 'dict': 0.16; 'entries.': 0.16; 'letter:': 0.16; 'lookups': 0.16; 'subject:values': 0.16; 'thread,': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'wed,': 0.18; '(the': 0.22; 'example': 0.22; 'initial': 0.24; 'post': 0.26; 'subject:/': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'tim': 0.29; 'words': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'chase': 0.31; "d'aprano": 0.31; 'keys': 0.31; 'steven': 0.31; 'ago': 0.33; 'except': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'starting': 0.37; 'to:addr:python-list': 0.38; 'previous': 0.38; 'does': 0.39; 'structure': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'space': 0.40; '2nd': 0.60; 'new': 0.61; 'first': 0.61; 'save': 0.62; 'more': 0.64; 'deeply': 0.69; 'potentially': 0.81; 'dict,': 0.84; 'dict.': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=zL7NGBqXi7B4orj9FGy7Z7/WjPYpTJLjl0w7lcBjw6I=; b=KJpyO8btheAuH7hi0kzCoG5mfpGvUXfnfodMCoiunQ9+41Oq1DKJRlGdUkZdrBilSs AxGZU3sZ5Mwp8DMExTtA+TXHQLo0hDG1MtCr1mu7PRcMV3Cc50HAI1iJU/5vhcYT+6wI pWMKDnDZm6FmId4Evh2ow11v1C7cpw3UH5tUhKDXgf/aTKJNgJ93Y6FGt9cJ7oOcLjTF sSt3sub935TzGPj4jja1sFGbSPkVplGOhxt3hSYcMgXJGjIfDZ1F5LoahCFcBeNi82+l f/o2GpsA9iY0hhhEfpyzWhEBarVKLPkO0hUEOJqGJl6FrAS9DYrMDBGmPE5vOZhc+LdH 35ug== X-Received: by 10.52.172.68 with SMTP id ba4mr1070363vdc.66.1386810202349; Wed, 11 Dec 2013 17:03:22 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <20131211083051.65e3d501@bigbox.christie.dr> References: <52A7AB8C.8030700@arcor.de> <52a86c57$0$29992$c3e8da3$5496439d@news.astraweb.com> <20131211083051.65e3d501@bigbox.christie.dr> From: Ian Kelly Date: Wed, 11 Dec 2013 18:02:42 -0700 Subject: Re: grab dict keys/values without iterating ?! To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386810211 news.xs4all.nl 2879 [2001:888:2000:d::a6]:50085 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61635 On Wed, Dec 11, 2013 at 7:30 AM, Tim Chase 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.