Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ndarray如何使用简单的函数,如x**2?_Python_Numpy - Fatal编程技术网

Python ndarray如何使用简单的函数,如x**2?

Python ndarray如何使用简单的函数,如x**2?,python,numpy,Python,Numpy,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,

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]
类信息(对象):
定义初始化(self,x):
self.a=x<
定义功率(自我,x):
对于x范围内的i(len(self.a)):
self.a[i]**=x
回归自我
a=信息([3,4])
f=λx:x**2
a=f(a)
印刷a.a
[9, 16]
因为对于
**
运算符,而列表元组不包含任何对象(它们可能包含任何对象,而nArray通常用于数字)