[英]Loop through a range in x steps?
I have the radius of a circle and the equation y = int(math.sqrt(pow(radius, 2)-pow(i,2)))
(Pythagoras). 我有一个圆的半径和等式
y = int(math.sqrt(pow(radius, 2)-pow(i,2)))
(毕达哥拉斯)。
Now I want to loop over the range range(-radius,0) + range(1, radius+1)
within a multiple of 16 steps like this: 现在,我想像这样在16个步骤的倍数内循环范围
range(-radius,0) + range(1, radius+1)
:
radius = 4
Actually output: -4, -3, -2, 1, 1, 2, 3, 4
What I want: -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4
And I found out, that you can't change the step-size of for-loops easily but I found a way: 我发现,您无法轻松更改for循环的步长,但我找到了一种方法:
print("Length: ", len([x/2 for x in chain(range(-2*radius, 0), range(1,2*radius+1))]))
print("Values: ", [x/2 for x in chain(range(-2*radius, 0), range(1,2*radius+1))])
Which returns 16
and the wanted "count", as I mentioned above. 如上所述,它返回
16
和所需的“计数”。
Now my question is : How can I automate this? 现在我的问题是 :我该如何自动化? Because that it works with 4 is just a "coincidence" and with eg
radius = 5
it won't work like this. 因为它与4一起工作只是一个“巧合”,而与例如
radius = 5
它就不会这样工作。 So is there a solution that I can loop through a range in x steps? 那么,有没有一种解决方案可以让我逐步扩展x范围呢?
Solution 1.1: 解决方案1.1:
You can use the linspace
-function and then pick those values which are not 0. This is done by the my_range
-function in the following code: 您可以使用
linspace
-function,然后选择那些不为0的值。这是由my_range
在以下代码中完成的:
import numpy as np
def my_range(radius, steps):
ran = np.linspace(-radius, radius, steps+1)
return ran[np.nonzero(ran)]
Now, 现在,
[i for i in my_range(4, 16)]
produces 产生
[-4.0, -3.5, -3.0, -2.5, -2.0, -1.5, -1.0, -0.5, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]
Solution 1.2: 解决方案1.2:
Alternatively, 或者,
list(my_range(4, 16))
produces the same list. 产生相同的列表。
Solution 2: 解决方案2:
If you don't want to define a function, here is a concise way to achieve the same thing: 如果您不想定义一个函数,这是一种实现同一目的的简洁方法:
[i for i in np.linspace(-4, 4, 16+1) if i != 0]
Hope you like this solution, __future__
import is for python 2.x: 希望您喜欢这个解决方案,
__future__
import适用于python 2.x:
from __future__ import division
r = 4
result = [-a/2 for a in xrange(1, r*2+1)][::-1] + [a/2 for a in xrange(1, r*2+1)]
print result
it looks like you want +/- limits equal magnitude 看起来您想要+/-限制大小相等
you then have the choice of include the endpoint in the range, and if that then makes you want to get n + 1 points 然后,您可以选择将端点包括在范围内,如果这样,则您希望获得n + 1点
pure python, no imports: 纯python,不导入:
lim, n = 3, 10
[-lim + i * 2 * lim / (n - 1) for i in range(n)]
Out[27]:
[-3.0,
-2.3333333333333335,
-1.6666666666666667,
-1.0,
-0.3333333333333335,
0.3333333333333335,
1.0,
1.666666666666667,
2.333333333333333,
3.0]
or 要么
[-lim + i * 2 * lim / n for i in range( n + 1)]
Out[28]:
[-3.0,
-2.4,
-1.8,
-1.2,
-0.6000000000000001,
0.0,
0.6000000000000001,
1.2000000000000002,
1.7999999999999998,
2.4000000000000004,
3.0]
changing the list comprehension sq brackets to round makes a generator that could be used in place of range() 将列表理解方括号更改为圆形可生成可用于代替range()的生成器
range function takes three parameters start , stop and step (third argument is optional) . range函数采用三个参数start,stop和step(第三个参数是可选的)。
range(-4,4,0.5)
will solve your problem. range(-4,4,0.5)
将解决您的问题。 so let's assume -x is start and x is stop. 因此,让我们假设-x是start并且x是stop。
step = int(x/16);
range(-x, x, step);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.