map/filter/reduce/lambda opinions and background unscientificmini-survey

Stian Søiland stian at soiland.no
Wed Jul 6 15:16:40 EDT 2005


On 2005-07-06 07:00:04, Steven D'Aprano wrote:

> map(lambda x: if x == 0: 1; else: math.sin(x)/x,
>      myList)

And now for the "readable" list comprehension version:

    [x==0 and 1 or math.sin(x)/x for x in myList]

Now even though I'm into the short-circuiting of and-or and even
occasionally have used such masturbation techniques as this, I don't
think it qualifies as pythonic.

If it was me, I would probably even have written:

    [x and math.sin(x)/x or 1 for x in myList]

-- 
Stian Søiland               Work toward win-win situation. Win-lose
Trondheim, Norway           is where you win and the other lose.
http://soiland.no/          Lose-lose and lose-win are left as an
                            exercise to the reader.  [Limoncelli/Hogan]



More information about the Python-list mailing list