From: aahz at netcom.com (Aahz Maruch)
Date: Tue, 27 Apr 1999 23:17:23 GMT
Subject: try vs. has_key()
References: <aahzFAM4oJ.M7M@netcom.com> <yWOT2.6007$8m5.9320@newsr1.twcny.rr.com> <Pine.SUN.3.95-heb-2.07.990423140345.21577A-100000@sunset.ma.huji.ac.il> <7g51q6$1pt$1@vvs.superst.iae.nl>
Message-ID: <aahzFAvFCz.3Lr@netcom.com>
Content-Length: 1267
X-UID: 1276                                                  

In article <7g51q6$1pt$1 at vvs.superst.iae.nl>,
Carel Fellinger  <cfelling at iae.nl> wrote:
>In article <Pine.SUN.3.95-heb-2.07.990423140345.21577A-100000 at sunset.ma.huji.ac.il> you wrote:
>>
>> d={}
>> for word in words:
>> 	first_two=word[:2]
>> 	d[first_two]=d.get(first_two, []).append(word)
>
>this is the statement where i get lost. and when i try it with python 1.5.1
>it doesn't work either. As far as i understand the append function it doesn't
>return anything as it is just a shorthand for an assignment which to my
>knowledge even in python 1.5.2 doesn't return values. or am i missing
>something here? nonetheless, it looks great, and i sure hope it works too.

It appears that the second parameter for get() (to specify a value
different from None when the key is not found) is new to 1.5.2.  In
1.5.1 you still have to do the following:

d={}
for word in words:
 	first_two=word[:2]
	if d.get(first_two) is None :
 		d[first_two]=[word]
	else :
 		d[first_two].append(word)

(I think, without testing the code)
-- 
                      --- Aahz (@netcom.com)

Hugs and backrubs -- I break Rule 6       <*>      http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het

Hi!  I'm a beta for SigVirus 2000!  Copy me into your .sigfile!




