Re: [Tutor] Bigrams and nested dictionaries

2006-04-04 Thread Kent Johnson
Michael Broe wrote: > dict = {} dict is the name of the builtin dictionary class, so you shouldn't use it as the name of your dict - you shadow the built-in name. file is also a built-in name. > > L = file[0] > for R in file[1:]:# move right edge of window across the file > if not L

Re: [Tutor] Bigrams and nested dictionaries

2006-04-03 Thread Orri Ganel
My only comment is that this considers spaces and punctuation (like parentheses, brackets, etc.), too, which I assume you don't want seeing as how that has little to do with natural languages.  My suggestion would be to remove the any punctuation or whitespace keys from the dictionary after you've

Re: [Tutor] Bigrams and nested dictionaries

2006-04-03 Thread Michael Broe
Well coming up with this has made me really love Python. I worked on this with my online pythonpenpal Kyle, and here is what we came up with. Thanks to all for input so far. My first idea was to use a C-type indexing for-loop, to grab a two- element sequence [i, i+1]: dict = {} for i in rang

Re: [Tutor] Bigrams and nested dictionaries

2006-04-03 Thread Victor Bouffier
On Mon, 2006-04-03 at 11:39 -0700, Danny Yoo wrote: > > You can check if the dictionary key exists prior to assigning to it: > > > > >>> if not D.has_key('c'): > > ...D['c'] = {} > > >>> D['c']['a'] = 1 > > > Hi Victor, > > Another approach is to use the badly-named "setdefault()" method whi

Re: [Tutor] Bigrams and nested dictionaries

2006-04-03 Thread Alan Gauld
> On Wed, 2006-03-29 at 00:15 -0500, Michael Broe wrote: > You can check if the dictionary key exists prior to assigning to it: > if not D.has_key('c'): > ...D['c'] = {} D['c']['a'] = 1 And since 2.4? if 'c' in D: ... Alan G. ___ Tutor

Re: [Tutor] Bigrams and nested dictionaries

2006-04-03 Thread Danny Yoo
> You can check if the dictionary key exists prior to assigning to it: > > >>> if not D.has_key('c'): > ...D['c'] = {} > >>> D['c']['a'] = 1 Hi Victor, Another approach is to use the badly-named "setdefault()" method which is a close analogue to Perl's "autovivification" feature: ## >>>

Re: [Tutor] Bigrams and nested dictionaries

2006-04-03 Thread Victor Bouffier
On Wed, 2006-03-29 at 00:15 -0500, Michael Broe wrote: > Aha! John wrote: > > "Are you sure you haven't mistakenly assigned something other than a > dict to D or D['d'] ?" > > Thanks for the tip! Yup that was it (and apologies for not reporting > the problem more precisely). I hadn't initiali

Re: [Tutor] Bigrams and nested dictionaries

2006-03-28 Thread Michael Broe
Aha! John wrote: "Are you sure you haven't mistakenly assigned something other than a dict to D or D['d'] ?" Thanks for the tip! Yup that was it (and apologies for not reporting the problem more precisely). I hadn't initialized the nested dictionary before trying to assign to it. (I think P

Re: [Tutor] Bigrams and nested dictionaries

2006-03-28 Thread Kent Johnson
Michael Broe wrote: > I'm playing with the whole idea of creating bigram (digram?) > frequencies for text analysis and cryptographic and entropy analysis > etc (this is as much an exercise in learning Python and programming > as anything else, I realise everything has already been done > so

Re: [Tutor] Bigrams and nested dictionaries

2006-03-28 Thread John Fouhy
On 29/03/06, Michael Broe <[EMAIL PROTECTED]> wrote: > Well I ran into an interesting glitch already. For a dictionary D, I > can pull out a nested value using this syntax: > > >>> D['b']['a'] > 23 > > and I can assign to this dictionary using > > >>> D['d'] = {'a':7, 'b':0'} > > but I can't assi

Re: [Tutor] Bigrams and nested dictionaries

2006-03-28 Thread Michael Broe
Well I ran into an interesting glitch already. For a dictionary D, I can pull out a nested value using this syntax: >>> D['b']['a'] 23 and I can assign to this dictionary using >>> D['d'] = {'a':7, 'b':0'} but I can't assign like this: >>> D['d']['c'] = 1 TypeError: object does not suppor