我已经迭代定义了一个包含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)}
但是,i
inmath.cos(2*math.pi*i/360)
不会在范围上进行迭代。如果我打印xCoords["i"][0]
超出范围(0,360)
,则每张纸都得到0 i > 1
。如何i
在表达式中进行更改?
该i
实际上是遍历值如预期。这里的问题是您使用as dtype
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句