How can I perform in Python memberwise operation on list (vector) such as in Matlab/Octave. I mean, giving two lists:
a = [1,2,3]
b = [4,5,6]
Are there memberwise operators such as:
c = a .* b = [1*4,2*5,3*6]
d = a ./ b = [1/4,2/5,3/6]
I know that I can implement it by myself, and I will do so if there is not such module. A not completely bulletproof way could be:
# Memberwise product:
def mwprod(a,b):
c = []
if len(a) == len(b):
for a,b in zip(a,b):
try:
c.append(a*b)
except:
c.append(NaN)
return c
As stated by user3426575 this can be condensed in a very pythonic way:
c = [ x*y for x, y in zip(a,b) ]
Anyway I am wondering for something more dense and aesthetic which overloads or implements such operators on list.