Re: Keeping track of things with dictionaries

2014-04-09 Thread Gene Heskett
On Wednesday 09 April 2014 05:47:37 Ian Kelly did opine: On Tue, Apr 8, 2014 at 9:31 PM, Gene Heskett ghesk...@wdtv.com wrote: 'Pneumonoultramicroscopicsilicovolcanoconiosis' has them all beat. Source citation please?

Re: Keeping track of things with dictionaries

2014-04-08 Thread Josh English
On Monday, April 7, 2014 9:08:23 PM UTC-7, Chris Angelico wrote: That depends on whether calling Brand() unnecessarily is a problem. Using setdefault() is handy when you're working with a simple list or something, but if calling Brand() is costly, or (worse) if it has side effects that you

Re: Keeping track of things with dictionaries

2014-04-08 Thread Frank Millman
Chris Angelico ros...@gmail.com wrote in message news:captjjmqfbt2xx+bdfnhz0gagordkhtpbzrr29duwn36girz...@mail.gmail.com... On Tue, Apr 8, 2014 at 2:02 PM, Josh English joshua.r.engl...@gmail.com wrote: Would dict.setdefault() solve this problem? Is there any advantage to defaultdict over

Re: Keeping track of things with dictionaries

2014-04-08 Thread Steven D'Aprano
On Tue, 08 Apr 2014 09:14:39 +0200, Frank Millman wrote: It appears that when you use 'setdefault', the default is always evaluated, even if the key exists. def get_value(val): ... print('getting value', val) ... return val*2 ... my_dict = {} my_dict.setdefault('a',

Re: Keeping track of things with dictionaries

2014-04-08 Thread Ian Kelly
On Tue, Apr 8, 2014 at 1:14 AM, Frank Millman fr...@chagford.com wrote: Chris Angelico ros...@gmail.com wrote in message news:captjjmqfbt2xx+bdfnhz0gagordkhtpbzrr29duwn36girz...@mail.gmail.com... On Tue, Apr 8, 2014 at 2:02 PM, Josh English joshua.r.engl...@gmail.com wrote: Would

Re: Keeping track of things with dictionaries

2014-04-08 Thread Chris Angelico
On Tue, Apr 8, 2014 at 5:14 PM, Frank Millman fr...@chagford.com wrote: It appears that when you use 'setdefault', the default is always evaluated, even if the key exists. def get_value(val): ... print('getting value', val) ... return val*2 ... my_dict = {} my_dict.setdefault('a',

Re: Keeping track of things with dictionaries

2014-04-08 Thread Peter Otten
Ian Kelly wrote: One thing I will note as a disadvantage of defaultdict is that sometimes you only want the default value behavior while you're initially building the dict, and then you just want a normal dict with KeyErrors from then on. defaultdict doesn't do that; once constructed, it

Re: Keeping track of things with dictionaries

2014-04-08 Thread Frank Millman
Chris Angelico ros...@gmail.com wrote in message news:captjjmpk-rqx0fp6_4vxyus2z34vc5fq_qntj+q9+kn8y5u...@mail.gmail.com... On Tue, Apr 8, 2014 at 5:14 PM, Frank Millman fr...@chagford.com wrote: It appears that when you use 'setdefault', the default is always evaluated, even if the key

Re: Keeping track of things with dictionaries

2014-04-08 Thread Frank Millman
Ian Kelly ian.g.ke...@gmail.com wrote in message news:CALwzidmP5Bevbace9GyQrVXe-_2T=jtpq1yvapsaepvomqe...@mail.gmail.com... On Tue, Apr 8, 2014 at 1:14 AM, Frank Millman fr...@chagford.com wrote: It appears that when you use 'setdefault', the default is always evaluated, even if the key

Re: Keeping track of things with dictionaries

2014-04-08 Thread Chris Angelico
On Tue, Apr 8, 2014 at 6:26 PM, Frank Millman fr...@chagford.com 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

Re: Keeping track of things with dictionaries

2014-04-08 Thread Frank Millman
Chris Angelico ros...@gmail.com wrote in message news:CAPTjJmoRxEhX02ZviHiLO+qi+dD+81smbGGYcPECpHb5E=p4=a...@mail.gmail.com... On Tue, Apr 8, 2014 at 6:26 PM, Frank Millman fr...@chagford.com wrote: words_by_length = {} for word in open(/usr/share/dict/words):

Re: Keeping track of things with dictionaries

2014-04-08 Thread Chris Angelico
On Tue, Apr 8, 2014 at 7:28 PM, Frank Millman fr...@chagford.com wrote: Are you saying that all([len(word) == 23 for word in words_by_length[23]]) # hope I got that right will not return True? That'll return true. What it won't show, though, is the length of the word as you would

Re: Keeping track of things with dictionaries

2014-04-08 Thread Frank Millman
Chris Angelico ros...@gmail.com wrote in message news:captjjmppaqmb6no7udddadqg_jv9yz0sn4d70kasksbwwr3...@mail.gmail.com... On Tue, Apr 8, 2014 at 7:28 PM, Frank Millman fr...@chagford.com wrote: Are you saying that all([len(word) == 23 for word in words_by_length[23]]) # hope I got

Re: Keeping track of things with dictionaries

2014-04-08 Thread Gregory Ewing
Chris Angelico wrote: 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. Mine's bigger than yours! On MacOSX 10.6 I get 41 words. (I think someone must have

Re: Keeping track of things with dictionaries

2014-04-08 Thread Chris Angelico
On Wed, Apr 9, 2014 at 10:43 AM, Gregory Ewing greg.ew...@canterbury.ac.nz wrote: Chris Angelico wrote: 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.

Re: Keeping track of things with dictionaries

2014-04-08 Thread alex23
On 8/04/2014 6:31 PM, Frank Millman wrote: Here is an idea, inspired by Peter Otten's suggestion earlier in this thread. Instead of defaultdict, subclass dict and use __missing__() to supply the default values. When the dictionary is set up, delete __missing__ from the subclass! Ugly, but it

Re: Keeping track of things with dictionaries

2014-04-08 Thread alex23
On 9/04/2014 12:33 PM, Chris Angelico wrote: Unfortunately I seem to be missing antidisestablishmentarianism, because the longest words in my dict are only 24 characters, excluding the '\n'. Should I ask for my money back? I think you should. That's a fundamental flaw in the dictionary.

Re: Keeping track of things with dictionaries

2014-04-08 Thread Ian Kelly
On Tue, Apr 8, 2014 at 8:45 PM, alex23 wuwe...@gmail.com wrote: On 9/04/2014 12:33 PM, Chris Angelico wrote: Unfortunately I seem to be missing antidisestablishmentarianism, because the longest words in my dict are only 24 characters, excluding the '\n'. Should I ask for my money back? I

Re: Keeping track of things with dictionaries

2014-04-08 Thread Gene Heskett
On Tuesday 08 April 2014 23:31:35 Ian Kelly did opine: On Tue, Apr 8, 2014 at 8:45 PM, alex23 wuwe...@gmail.com wrote: On 9/04/2014 12:33 PM, Chris Angelico wrote: Unfortunately I seem to be missing antidisestablishmentarianism, because the longest words in my dict are only 24 characters,

Re: Keeping track of things with dictionaries

2014-04-08 Thread Ian Kelly
On Tue, Apr 8, 2014 at 9:31 PM, Gene Heskett ghesk...@wdtv.com wrote: 'Pneumonoultramicroscopicsilicovolcanoconiosis' has them all beat. Source citation please? http://en.wikipedia.org/wiki/Pneumonoultramicroscopicsilicovolcanoconiosis

Re: Keeping track of things with dictionaries

2014-04-07 Thread Josh English
On Sunday, April 6, 2014 12:44:13 AM UTC-7, Giuliano Bertoletti wrote: obj = brands_seen.get(brandname) if obj is None: obj = Brand() brands_seen[brandname] = obj Would dict.setdefault() solve this problem? Is there any advantage to defaultdict over setdefault() Josh --

Re: Keeping track of things with dictionaries

2014-04-07 Thread Chris Angelico
On Tue, Apr 8, 2014 at 2:02 PM, Josh English joshua.r.engl...@gmail.com wrote: On Sunday, April 6, 2014 12:44:13 AM UTC-7, Giuliano Bertoletti wrote: obj = brands_seen.get(brandname) if obj is None: obj = Brand() brands_seen[brandname] = obj Would dict.setdefault() solve this

Keeping track of things with dictionaries

2014-04-06 Thread Giuliano Bertoletti
I frequently use this pattern to keep track of incoming data (for example, to sum up sales of a specific brand): = # read a brand record from a db ... # keep track of brands seen obj = brands_seen.get(brandname) if obj is None: obj = Brand()

Re: Keeping track of things with dictionaries

2014-04-06 Thread Peter Otten
Giuliano Bertoletti wrote: I frequently use this pattern to keep track of incoming data (for example, to sum up sales of a specific brand): = # read a brand record from a db ... # keep track of brands seen obj = brands_seen.get(brandname) if obj