On Sat, Feb 2, 2019 at 3:23 PM Christopher Barker <pythonchb@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)


[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)]


    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.


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])]


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