You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

42 lines
1.2 KiB

From: William.H.Duquette at jpl.nasa.gov (William H. Duquette)
Date: Wed, 28 Apr 1999 14:58:13 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>
Message-ID: <37247ea3.494305@news.jpl.nasa.gov>
X-UID: 312
On Fri, 23 Apr 1999 14:13:59 +0300, Moshe Zadka
<moshez at math.huji.ac.il> wrote:
>d={}
>for word in words:
> first_two=word[:2]
> d[first_two]=d.get(first_two, []).append(word)
>
>Unfortunately, few people seem to know about the ``get'' method, which
>is really good.
This doesn't seem to work. For example, here's a python
interpreter session:
>>> d = {}
>>> a = 'Foo'
>>> d[a] = d.get(a, []).append('Bar')
>>> d
{'Foo': None}
>>>
I'd have expected to see {'Foo': 'Bar'}, but that's not what I get.
I'm using Python 1.5.2, by the way.
Will Duquette
--------------------------------------------------------------------------
Will Duquette, JPL | William.H.Duquette at jpl.nasa.gov
But I speak only | http://eis.jpl.nasa.gov/~will (JPL Use Only)
for myself. | It's amazing what you can do with the right tools.