Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'will,': 0.09; 'python': 0.11; 'bug': 0.12; 'wrote': 0.14; "(it's": 0.16; 'buggy': 0.16; 'hint?': 0.16; 'length.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'spotted': 0.16; 'subject:dictionaries': 0.16; 'symlink': 0.16; 'throw': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; '>>>': 0.22; 'saying': 0.22; 'entries': 0.24; "haven't": 0.24; 'header:X-Complaints- To:1': 0.27; 'unix': 0.29; 'words': 0.29; 'lines': 0.31; 'with,': 0.31; 'file': 0.32; 'run': 0.32; 'subject:with': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'list': 0.37; 'list.': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'letters': 0.60; 'problems.': 0.60; 'hope': 0.61; 'skip:o 30': 0.61; "you're": 0.61; 'name': 0.63; 'american': 0.66; 'here': 0.66; 'frank': 0.68; 'skip:w 30': 0.69; 'skip:/ 30': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Keeping track of things with dictionaries Date: Tue, 8 Apr 2014 11:28:19 +0200 References: <534105ce$0$1365$4fafbaef@reader1.news.tin.it> <21ef5159-ad95-4d43-a2d6-7ecda941d978@googlegroups.com> X-Gmane-NNTP-Posting-Host: 197.87.50.147 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 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: 1396949313 news.xs4all.nl 2916 [2001:888:2000:d::a6]:54279 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69848 "Chris Angelico" wrote in message news:CAPTjJmoRxEhX02ZviHiLO+qi+dD+81smbGGYcPECpHb5E=p4=A@mail.gmail.com... > 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. > I don't have a large dictionary to test with, and a small list of words (ls /etc > dict) did not throw up any problems. Are you saying that all([len(word) == 23 for word in words_by_length[23]]) # hope I got that right will not return True? Frank