[Tutor] Multifunction mapping

glidedon glidedon <glidedon@c-zone.net>
Sat, 04 May 2002 13:40:29 -0700


>Shorter is not better. Clearer is better. Something can be obfuscated both
>by making it too short and by making it too verbose.
>
>The shortest way to do this is
>
>L = "abcdefghij"
>
>s = ''.join(map(lambda x: '%03d' % ord(x), L))
>
>or with a list comprehension, even shorter:
>
>s = ''.join(['%03d' % ord(x) for x in L])
>
>Personally I don't think this is the best way though - too much happens on
>one line. I'd prefer
>
>import string
>
>L = "abcdefghij"
>ords = [ord(x) for x in L]
>strs = ['%03d' % o for o in ords]
>s = string.join(strs, '')
>
>Although maybe this is slightly too much on the verbose side, it is easier
>to see which steps are taken.
>
>Note the use of the % string formatting operator to get a number that's
>filled up with leading zeroes, and string.join(list, '') or ''.join(list) to
>join the parts together with nothing in between.
>
>-- 
>Remco Gerlich


Remco,

Excellent post, especially that last parargraph. Even I can follow that !

Thanks for tutoring !

Don