我正在尝试创建一个输出以下内容的函数:
[1,cos(t),sin(t),cos(2*t),sin(2*t),...,cos(n*t),sin(n*t) ]
称为 row_func
,接受 t 和 n 作为输入。
这是我到目前为止的代码:
def row_func(t,n):
L=0
g=np.cos()
h=np.sin()
L=[f(k) for k in range(n,t) for f in [g,h]]
L.insert(0,1)
return L
例如,当我使用如下输入:row_func(1,5)
时,它会抛出一个错误,指出参数数量无效。
我也知道 n 所在的位置无法实现示例中的功能,但我不知道如何将其合并。
提前致谢。
最佳答案
一个简单的循环就可以完成这项工作:
import math
def row_func(t, n):
out = [1]
for k in range(n + 1):
out.append(math.cos(k * t))
out.append(math.sin(k * t))
return out
关于python - 在 for 循环中调用变量来创建列表(使用 np.sin/np.cos),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54964117/