Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.040 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'will,': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'bug': 0.12; "(it's": 0.16; 'buggy': 0.16; 'closed.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hint?': 0.16; 'length.': 0.16; 'ought': 0.16; 'spotted': 0.16; 'subject:dictionaries': 0.16; 'symlink': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'pointed': 0.19; 'properly': 0.19; 'cc:addr:python.org': 0.22; 'entries': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; '[1]': 0.29; 'unix': 0.29; 'words': 0.29; 'message- id:@mail.gmail.com': 0.30; 'lines': 0.31; 'file': 0.32; 'run': 0.32; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'example,': 0.37; 'list': 0.37; 'list.': 0.37; 'skip:[ 10': 0.38; 'pm,': 0.38; 'little': 0.38; 'how': 0.40; 'ensure': 0.60; 'letters': 0.60; 'skip:o 30': 0.61; 'simple': 0.61; "you're": 0.61; 'name': 0.63; 'american': 0.66; 'here': 0.66; 'frank': 0.68; 'skip:w 30': 0.69; "'with'": 0.84; 'bug!': 0.84; 'julia': 0.84; 'skip:/ 30': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=NUVsK/PsNPMwuAjOXyo8bNqeVOqDoYqPL0tE0qhW33s=; b=MXIIFyCFZsFhuw0QUJQcRE8f4xcT2/YuIHqun/P1TiKwzoITWqk6TQ6RLfoxYaZXaw LXE/XqbdhMk5TQ9gpgMtmtRUmrSnQkjANMvA5crFimqbZ6N0oBl1kJkTRoSn1v2BuVHf dHd7W/B50YnakzgZAoSCyS1948to15aPROAqmTb3bwi4MoucS51pdAwemce7aCtiFpms bCwcEuuwE+maEwfB41n+1MSeFkXJBsE57lUjw7eP90t5LRTXOcD6WJQ3VNl1ak+KsSXJ Fr4ReVLUsG9IdMLwAQWs2TVifB/4dLJS4gl+5macPxpMl7HyAHYMix/Zsq+Y4LmUFpkr 1lPA== MIME-Version: 1.0 X-Received: by 10.66.102.39 with SMTP id fl7mr2887464pab.43.1396946102890; Tue, 08 Apr 2014 01:35:02 -0700 (PDT) In-Reply-To: References: <534105ce$0$1365$4fafbaef@reader1.news.tin.it> <21ef5159-ad95-4d43-a2d6-7ecda941d978@googlegroups.com> Date: Tue, 8 Apr 2014 18:35:02 +1000 Subject: Re: Keeping track of things with dictionaries From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396946112 news.xs4all.nl 2862 [2001:888:2000:d::a6]:42421 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69847 On Tue, Apr 8, 2014 at 6:26 PM, Frank Millman wrote: >> words_by_length = {} >> for word in open("/usr/share/dict/words"): >> words_by_length.setdefault(len(word), []).append(word) >> >> This will, very conveniently, give you a list of all words of a >> particular length. (It's actually a little buggy but you get the >> idea.) >> > > Thanks, that is neat. > > I haven't spotted the bug yet! Can you give me a hint? Run those lines in interactive Python (and change the file name if you're not on Unix or if you don't have a dictionary at that path), and then look at what's in words_by_length[23] - in the dictionary I have here (Debian Wheezy, using an American English dictionary - it's a symlink to (ultimately) /usr/share/dict/american-english), there are five entries in that list. Count how many letters there are in them. Also, there's a technical bug [1] in that I ought to use 'with' to ensure that the file's properly closed. But for a simple example, that's not critical. ChrisA [1] As Julia Jellicoe pointed out, it's an awful thing to be haunted by a technical bug!