-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 : I am doing some data massage, minor mapping and filtering really : and i find that i have a lot of this kind of kludgy code: : : # -- ------------------------- : def filt_seq(inseq): : out_list = [] : for item in inseq: : # 38 needs to be mapped on to 34 as we don't have a 38 in our : grid : if item == 38: : item = item - 4 : out_list.append(item) : # We also don't have a 40, but we do have a 39. Map that : sucka : elif item == 40: : item = item - 1 : out_list.append(item) : # if we get values that fall in good places, just pass them : on to the out seq : else: : out_list.append(item) : return out_list : : # -- ------------------------- : : To do some basic jiggering of some out of range off grid data. : : There has to be a better, more elegant, more flexible less brute : force more pythonic way to do this kind of mapping no?
Have you discovered the map() builtin yet? I would imagine that others on this list will have some even more elegant and efficient solutions for you, but here's a possibility: def filt_seq( thing ): if thing == 38: thing = thing - 4 elif thing == 40: thing = thing - 1 return thing l = [ 1, 17, 12, 38, 4, 40, 17, 19 ] l = map( filt_seq, l ) Good luck, - -Martin - -- Martin A. Brown http://linux-ip.net/ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) Comment: pgf-0.72 (http://linux-ip.net/sw/pine-gpg-filter/) iD8DBQFKonmNHEoZD1iZ+YcRAme7AKDqThvHP6+3xbzBg1p48rxmY21ztgCdFYnA Z2YGRnW7HLm/1oroaCTmkgg= =G9w5 -----END PGP SIGNATURE----- _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor