[Tutor] mapping/filtering a sequence

kevin parks kp8 at mac.com
Sun Sep 6 10:29:44 CEST 2009


I actually find the map biz easier to get my head around than the list  
comp. I guess this makes it another good reason for me to be happy  
that map is apparently staying in after nearly being depreciated. I  
generally prefer list comp in every instance, but the idea of an if  
else construct within the conditional of a list comp really hurts my  
brain, but it is nice to see that, had map(), filter() and reduce()  
all gotten the boot, there is a way to do it with a list comp, opaque  
as it is.

Thanks to all who responded this.

-kp--





On Sep 6, 2009, at 5:29 AM, Mark Tolonen wrote:

>
> "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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list