rot13 in a more Pythonic style?

Paul Rubin http
Wed Feb 14 18:29:20 EST 2007


"Andy Dingley" <dingbat at codesmiths.com> writes:
> c_rot13 = lambdaf c : (((c, uc_rot13(c)) [c in
> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ']), lc_rot13(c) )[c in
> 'abcdefghijklmnopqrstuvwxyz']

Oh, I see what you mean, you have separate upper and lower case maps
and you're asking how to select one in an expression.  Pythonistas
seem to prefer using multiple statements:

  def c_rot13(c):
     if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': return uc_rot13(c)
     elif c in 'abcdefghijklmnopqrstuvwxyz': return lc_rot13(c)
     return c

You could use the new ternary expression though:

   c_rot13 = lambda c: \
                  uc_rot13(c) if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' else \
                   (lc_rot13(c) if c in 'abcdefghijklmnopqrstuvwxyz' else \
                    c)

if I have that right.



More information about the Python-list mailing list