python - 在字符串/数组中存储多个函数值 (Python)

标签 python string function

我正在尝试将多个函数值存储在一个数组中,以便在 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/

相关文章:

python - 如何使用pandas实现vlookup交叉引用

python - 失败循环 : asking the user if they wish to continue (Python)

arrays - 从数组创建字符串的函数 (Mathematica)

c++ - 在主代码中调用 .hpp 函数时出错

python - 如何将datetime对象转换为float? AttributeError : 'datetime.datetime' object has no attribute 'datetime_to_float'

用于 sanic 应用程序的 python unitests

c++ - 在 C++ 中拥有字符串资源有什么意义?

C - 用 getpid() 中的 ID 替换字符串中的 $$?

两个不同的函数可以使用相同的输入变量吗?

Javascript 设置变量和对象以及正确的语法(调试控制台)