[Tutor] mapping/filtering a sequence

Mark Tolonen metolone+gmane at gmail.com
Sat Sep 5 22:29:57 CEST 2009


"Douglas Philips" <dgou at mac.com> wrote in message 
news:9EE00578-6AF7-4C6C-9968-AF5F25A00E03 at mac.com...
> On 2009 Sep 5, at 12:22 PM, Mark Tolonen wrote:
>> As a list comp:
>>
>>>>> L=range(30,41)
>>>>> [{38:34,40:39}.get(n,n) for n in L]
>> [30, 31, 32, 33, 34, 35, 36, 37, 34, 39, 39]
>
>
> True, that is terse, but IMHO has maintainability issues. The mapping 
> data structure and the method of transformation (.get()) are tangled  in 
> with the transformation itself.
> Again, IMHO, unless this is a very short script, the maintenance  should 
> outweigh raw terseness.

This *was* a very short script.  My point was using a list comp.  If you 
want to nitpick, don't use lower-case L for variables either :^)

my_map = { 38: 34, 40: 39 }

def filter_item(item):
     return my_map.get(item, item)

L = [filteritem(n) for n in L]

Happy?

-Mark




More information about the Tutor mailing list