Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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/8/python-3.x/19.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 在for循环中调用变量以生成列表(使用np.sin/np.cos)_Python_Python 3.x_Numpy - Fatal编程技术网

Python 在for循环中调用变量以生成列表(使用np.sin/np.cos)

Python 在for循环中调用变量以生成列表(使用np.sin/np.cos),python,python-3.x,numpy,Python,Python 3.x,Numpy,我正在尝试创建一个输出以下内容的函数: [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

我正在尝试创建一个输出以下内容的函数:

[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
让我们清理范围:

In [200]: def row_func(t,n): 
     ...:  g=np.cos 
     ...:  h=np.sin 
     ...:  L=[f(k) for k in range(t,n) for f in [g,h]] 
     ...:  return L 
     ...:   
     ...:                                                                       
In [201]: row_func(1,5)                                                         
Out[201]: 
[0.5403023058681398,
 0.8414709848078965,
 -0.4161468365471424,
 0.9092974268256817,
 -0.9899924966004454,
 0.1411200080598672,
 -0.6536436208636119,
 -0.7568024953079282]
np.cos
与数组一起工作,因此我们可以删除最内部的迭代,将其替换为
arange

In [202]: def row_func(t,n): 
     ...:  g=np.cos 
     ...:  h=np.sin 
     ...:  L=[f(np.arange(t,n)) for f in [g,h]] 
     ...:  return L 
     ...:                                                                       
In [203]: row_func(1,5)                                                         
Out[203]: 
[array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362]),
 array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 ])]
或者更简洁地说:

In [207]: x = np.arange(1,5)                                                    
In [208]: [np.cos(x), np.sin(x)]                                                
Out[208]: 
[array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362]),
 array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 ])]

numpy函数上不应该有括号。你当时不是在给他们打电话。但是我在零指数时真的不知道该怎么做。。。(cos(0)=1,sin(0)=0)
In [202]: def row_func(t,n): 
     ...:  g=np.cos 
     ...:  h=np.sin 
     ...:  L=[f(np.arange(t,n)) for f in [g,h]] 
     ...:  return L 
     ...:                                                                       
In [203]: row_func(1,5)                                                         
Out[203]: 
[array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362]),
 array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 ])]
In [207]: x = np.arange(1,5)                                                    
In [208]: [np.cos(x), np.sin(x)]                                                
Out[208]: 
[array([ 0.54030231, -0.41614684, -0.9899925 , -0.65364362]),
 array([ 0.84147098,  0.90929743,  0.14112001, -0.7568025 ])]