Comments on Version 2, Draft Pep for Deprecating Builtins
Duncan Booth
duncan at NOSPAMrcp.co.uk
Mon Apr 29 09:40:18 EDT 2002
holger krekel <pyth at devel.trillke.net> wrote in
news:mailman.1020082610.7467.python-list at python.org:
> Btw, what is the best/shortest way to express 'filter' and 'reduce' with
> list comprehensions?
filter(fn, lst)
becomes:
[ v for v in lst if fn(v) ]
filter(None, lst)
becomes:
[ v for v in lst if v ]
If the second argument to filter is a string or tuple then you have to
convert the resulting list back to the appropriate type:
e.g.
filter(fn, aString)
str.join('', [ v for v in aString if fn(v) ])
reduce doesn't return a list, so a list comprehension is not an appropriate
replacement.
reduce(fn, seq, initial)
becomes:
res = initial
for v in seq: res = fn(res, v)
All of these replacements are big winners if they remove a use of the 'l'
word. I don't think any of them lose in clarity even if there are no 'l's
around.
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list