[Python-ideas] Vectorization [was Re: Add list.join() please]
Steven D'Aprano
steve at pearwood.info
Thu Jan 31 18:23:08 EST 2019
On Thu, Jan 31, 2019 at 09:51:20AM -0800, Chris Barker via Python-ideas wrote:
> I do a lot of numerical programming, and used to use MATLAB and now numpy a
> lot. So I am very used to "vectorization" -- i.e. having operations that
> work on a whole collection of items at once.
[...]
> You can imagine that for more complex expressions the "vectorized" approach
> can make for much clearer and easier to parse code. Also much faster, which
> is what is usually talked about, but I think the readability is the bigger
> deal.
Julia has special "dot" vectorize operator that looks like this:
L .+ 1 # adds 1 to each item in L
func.(L) # calls f on each item in L
https://julialang.org/blog/2017/01/moredots
The beauty of this is that you can apply it to any function or operator
and the compiler will automatically vectorize it. The function doesn't
have to be written to specifically support vectorization.
> So what does this have to do with the topic at hand?
>
> I know that when I'm used to working with numpy and then need to do some
> string processing or some such, I find myself missing this "vectorization"
> -- if I want to do the same operation on a whole bunch of strings, why do I
> need to write a loop or comprehension or map? that is:
>
> [s.lower() for s in a_list_of_strings]
>
> rather than:
>
> a_list_of_strings.lower()
Using Julia syntax, that might become a_list_of_strings..lower(). If you
don't like the double dot, perhaps str.lower.(a_list_of_strings) would
be less ugly.
--
Steven
More information about the Python-ideas
mailing list