如何迭代调用在python中迭代定义的变量

我已经迭代定义了一个包含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)}

但是,iinmath.cos(2*math.pi*i/360)不会在范围上进行迭代。如果我打印xCoords["i"][0]超出范围(0,360),则每张纸都得到0 i > 1如何i在表达式中进行更改?

格伦DJ

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" % istr(i)因为它更易于阅读。输出示例:

>>> xCoords["1"][0]
0.9998476951563913

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章