[Tutor] mapping/filtering a sequence

kevin parks kp8 at mac.com
Wed Sep 9 13:54:58 CEST 2009


Prolly good to post final solutions for future goog'lerz (like when i  
forget) or anyone who was following along.

Here's where i ended up with this... shows both ways.
----------

#!/usr/bin/env python

my_map = { 38:34, 40:39, 45:44, 47:46, 52:51, 59:58, 55:56 }

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

# you can do it old skool with map()
def filter_test1():
     foo = range(60)
     mappedfoo = map(filter_item, foo)
     for item in foo:
         print foo[item], mappedfoo[item]

# you can also do it with a list comp
def filter_test2():
     foo = range(60)
     mappedfoo = [filter_item(n) for n in foo]
     for item in foo:
         print foo[item], mappedfoo[item]

if __name__ == "__main__":
     filter_test1()
     print "\n","------" * 8, "\n"
     filter_test2()

#--EOF--


More information about the Tutor mailing list