[Python-ideas] Respectively and its unpacking sentence

Chris Barker chris.barker at noaa.gov
Wed Jan 27 16:53:15 EST 2016


On Wed, Jan 27, 2016 at 9:12 AM, Mirmojtaba Gharibi <
mojtaba.gharibi at gmail.com> wrote:

>
>
> MATLAB has a built-in easy way of achieving component-wise operation and I
> think Python would benefit from that without use of libraries such as numpy.
>

I've always thought there should be a component-wise operations in Python.
The wlay to do it now is somthing like:

[i + j for i,j in zip(a,b)]

is really pretty darn wordy, compared to :

a_numpy_array + another_numpy array

(similar in matlab).

But maybe an operator is the way to do it. But it was long ago decide dnot
to introduce a full set of extra operators, alla matlab:

.+
.*
etc....

rather, it was realized that for numpy, which does element-wise operations
be default, matrix multiplication was really the only non-elementwise
operation widely used, so the new @ operator was added.

And we're kind of stuck --even if we added a full set, then in numpy, the
regular operators would be element wise, but for built-in Python sequences,
the special ones would be elementwise -- really confusing!

if you really want this, I'd make your own sequences that re-define the
operators.

Or just use Numpy... you can use object arrays if you want to handle
non-numeric values:

In [*4*]: a1 = np.array(["this", "that"], dtype=object)

In [*5*]: a2 = np.array(["some", "more"], dtype=object)

In [*6*]: a1 + a2

Out[*6*]: array(['thissome', 'thatmore'], dtype=object)
-CHB


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160127/d086c3ec/attachment-0001.html>


More information about the Python-ideas mailing list