如何迭代调用python中迭代定义的变量
我反复定义了一个包含x坐标的变量,如下所示如何迭代调用python中迭代定义的变量,python,numpy,Python,Numpy,我反复定义了一个包含x坐标的变量,如下所示 import numpy as np xCoords = {"%s" % i: np.array([math.cos(2*math.pi*i/360),0,0,0,0,0,0,0,0,0], dtype = int) for i in range(0, 360)} 但是,math.cos(2*math.pi*i/360)中的i不会在该范围内迭代。如果我在(0360)范围内打印xCoords[“I”][0],则每个I>1得到0。如何
import numpy as np
xCoords = {"%s" % i: np.array([math.cos(2*math.pi*i/360),0,0,0,0,0,0,0,0,0], dtype = int) for i in range(0, 360)}
但是,
math.cos(2*math.pi*i/360)
中的i
不会在该范围内迭代。如果我在(0360)
范围内打印xCoords[“I”][0]
,则每个I>1
得到0。如何使I
在表达式中发生更改?实际上,I
按预期对值进行迭代。这里的问题是您采用asdtype
int
,这会导致将所有值转换为整数。如果该值介于0和1之间,则结果为0
如果使用例如float
,则不会出现此问题:
xCoords = {str(i): np.array([math.cos(2*math.pi*i/360),0,0,0,0,0,0,0,0,0], dtype = float) for i in range(0, 360)}
我还将%s”%I
替换为str(I)
,因为它更容易阅读。示例输出:
>>> xCoords["1"][0]
0.9998476951563913