scipy中的 ndarray 是怎么 将一个向量用于一个 普通的函数的?如f = lambda x: x**2
arr = array([3,4,5])<br>
f = lambda x: x**2<br>
print f(arr) # works, but how?<br>
print f(3) # works<br>
print f([3,4,5]) # will not work<br>
#TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'<br>
print f((3,4,5)) # will not work<br>
#TypeError: unsupported operand type(s) for ** or pow(): 'tuple' and 'int'<br>
>>>f(arr)
[ 9 16 25]
>>>f(3)
9
scipy中的 ndarray 是怎么 将一个向量用于一个 普通的函数的?如f = lambda x: x**2
我自己放 list却不行 ?
请高手指教!!!
谢谢!!
[解决办法]
貌似不行吧
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> arr = [3,4,5]>>> f = lambda x: x ** 2>>> print f(3)9>>> print f(arr)Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> print f(arr) File "<pyshell#1>", line 1, in <lambda> f = lambda x: x ** 2TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'>>>
[解决办法]
$ python
Python 2.7.2+ (default, Oct 4 2011, 20:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> arr = range(3,6)
>>> arr
[3, 4, 5]
>>> f = lambda x: x**2
>>> map(f, arr)
[9, 16, 25]
>>>