ndarray如何使用简单的函数,比如x ** 2?我收到这个错误:
arr = array([3,4,5])
f = lambda x: x**2
print f(arr) # works, but how?
print f(3) # works
print f([3,4,5]) # will not work
#TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
print f((3,4,5)) # will not work
#TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'
>>>f(arr)
#[ 9 16 25]
>>>f(3)
#9
最后...
class Info(object):
def __init__(self,x):
self.a = x<
def __pow__(self,x):
for i in xrange(len(self.a)):
self.a[i]**=x
return self
a = Info([3,4])
f = lambda x:x**2
a = f(a)
print a.a
[9, 16]