[Python-ideas] Vectorization [was Re: Add list.join() please]
Ben Rudiak-Gould
benrudiak at gmail.com
Sat Feb 2 18:35:13 EST 2019
On Sat, Feb 2, 2019 at 3:23 PM Christopher Barker <pythonchb at gmail.com>
wrote:
> performance asside, I use numpy because:
>
> c = np.sqrt(a**2 + b**2)
>
> is a heck of a lot easer to read, write, and get correct than:
>
> c = list(map(math.sqrt, map(lambda x, y: x + y, map(lambda x: x**2, a),
> map(lambda x: x**2, b)
> )))
>
> or:
>
> [math.sqrt(x) for x in (a + b for a, b in zip((x**2 for x in a),
> (x**2 for x in b)
> ))]
>
You can also write
c = [math.sqrt(x**2 + y**2) for x, y in zip(a, b)]
or
c = list(map(lambda x, y: math.sqrt(x**2 + y**2), a, b))
or, since math.hypot exists,
c = list(map(math.hypot, a, b))
In recent Python versions you can write [*map(...)] instead of
list(map(...)), which I find more readable.
a_list_of_strings.strip().lower().title()
>
> is a lot nicer than:
>
> [s.title() for s in (s.lower() for s in [s.strip(s) for s in
> a_list_of_strings])]
>
> or
>
> list(map(str.title, (map(str.lower, (map(str.strip, a_list_of_strings))))
> # untested
>
In this case you can write
[s.strip().lower().title() for s in a_list_of_strings]
-- Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190202/f481d8bb/attachment.html>
More information about the Python-ideas
mailing list