wasm-demo/demo/ermis-f/python_m/cur/0219

39 lines
1.3 KiB
Plaintext

From: jeremy at cnri.reston.va.us (Jeremy Hylton)
Date: Fri, 30 Apr 1999 11:08:30 -0400 (EDT)
Subject: try vs. has_key()
In-Reply-To: <slrn7ii0vb.tdc.jepler@craie.inetnebr.com>
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>
<aahzFAvFCz.3Lr@netcom.com>
<14119.8145.293039.667256@bitdiddle.cnri.reston.va.us>
<slrn7ii0vb.tdc.jepler@craie.inetnebr.com>
Message-ID: <14121.50708.131339.251436@bitdiddle.cnri.reston.va.us>
X-UID: 219
>>>>> "JE" == Jeff Epler <jepler at inetnebr.com> writes:
>>The following code would be correct:
>>
>> d={}
>> for word in words:
>> first_two = word[:2]
>> d[first_two]= temp = d.get(first_two, [])
>> temp.append(word)
JE> what about d[first_two] = d.get(first_two, [])+[word] ? Or is
JE> list construction and addition going to be enough more expensive
JE> than the function call to make this a lose as well?
Yeah. Concatenating two lists results in a new list being created
every time (and you're already creating a new list containing the
value of word). Two list allocs is definitely more expensive that an
append.
Jeremy