On Apr 1, 5:10 pm, John Posner <jjpos...@snet.net> wrote: > Dennis Lee Bieber presented a code snippet with two consecutive statements > that made me think, "I'd code this differently". So just for fun ... is > Dennis's original statement or my "_alt" statement more idiomatically > Pythonic? Are there even more Pythonic alternative codings? > > mrkrs = [b for b in block > if b > 127 > or b in [ "\r", "\n", "\t" ] ] > > mrkrs_alt1 = filter(lambda b: b > 127 or b in [ "\r", "\n", "\t" ], > block) > mrkrs_alt2 = filter(lambda b: b > 127 or b in list("\r\n\t"), block) >
Never tested my 'pythonicity', but I would do: def test(b) : b > 127 or b in r"\r\n\t" mrkrs = filter( test, block ) Note: before starting to study haskell, I would probably have used the list comprehension. Still can't stand anonimous functions though. > (Note: Dennis's statement converts a string into a list; mine does not.) > > --- > > binary = (float(len(mrkrs)) / len(block)) > 0.30 > > binary_alt = 1.0 * len(mrkrs) / len(block) > 0.30 > I believe now one should do (at least on new code): from __future__ import division # not needed for python 3.0 binary = ( len( mrks) / len (blocks) ) > 3.0 In the past, I often used the * 1.0 trick, but nevertheless believe that it is better using explicit cast. > -John > Ciao ----- FB -- http://mail.python.org/mailman/listinfo/python-list