python中的成员运算符_python中的成员运算符(Memberwise operator in python)

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值