3 ms·
Ironically, map and filter are still available, when they seem easily duplicatible with comprehensions: map(f,lst) = [f(x) for x in lst] filter(f,lst) = [x
by jrp 18y ago
Ironically, map and filter are still available, when they seem easily duplicatible with comprehensions:
map(f,lst) = [f(x) for x in lst]
filter(f,lst) = [x for x in lst if f(x)]
But I don't see a way to translate reduce, except for perhaps the particular case of sum.
reduce(f,lst) = ???
- jacobolus 18y agoWrite a for loop. Much of the time, the code ends up clearer. See Guido's explanation at: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 http://www.artima.com/weblogs/viewpost.jsp?thread=98196
- jrp 18y agoThank you. I picked up Python from assorted online tutorials; maybe it's time for me to look at the book mentioned elsewhere in this thread to improve my style.
- wingi 18y agoThere existsa reason: the map/filter version can improved with a threaded map/filter variant, the list comprehension line not. And yes, a list comprehension produce a list with the same length, reduce not.