我正在尝试将多个函数值存储在一个数组中,以便在 python 中绘制函数图。
例如,我们定义函数为 y= x^2,所需的参数值为 1,2 和 3。我尝试的代码如下:
def ExampleFunction (x):
return x**2
ArgumentValues=range(3)
FunctionValues=ExampleFunction(ArgumentValues)
不幸的是,运行代码会导致错误:
TypeError: unsupported operand type(s) for ** or pow(): 'range' and 'int'
如何在 python 中将多个函数值返回到字符串/数组中?因此,我希望“函数值”采用以下形式:
1,4,9
最佳答案
使用列表理解:
results = [ExampleFunction(x) for x in range(3)]
关于python - 在字符串/数组中存储多个函数值 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37528903/