[Python-ideas] Vectorization [was Re: Add list.join() please]

Steven D'Aprano steve at pearwood.info
Sat Feb 2 18:40:23 EST 2019


On Sat, Feb 02, 2019 at 03:22:12PM -0800, Christopher Barker wrote:

[This bit was me] 
> Even numpy supports inhomogeneous data:
> > py> a = np.array([1, 'spam'])
> > py> a
> > array(['1', 'spam'],
> >       dtype='|S4')
> 
> 
> well, no -- it doesn't -- look carefully, that is an array or type '!S4' --
> i,e, a 4 element long string --every element in that array is that same
> type.

So it is. I wondered what the cryptic '|S4' symbol meant, and I 
completely missed the '' quotes around the 1.

Thanks for the correction.

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

Indeed. This hypothetical syntax brings the readability advantages of 
infix operators to code that operates on iterables, without requiring 
every iterable to support arbitrary functions and methods.




-- 
Steve


More information about the Python-ideas mailing list