简体   繁体   中英

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).
Now I want to loop over the range range(-radius,0) + range(1, radius+1) within a multiple of 16 steps like this:

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:

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.

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. So is there a solution that I can loop through a range in x steps?

Solution 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:

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:

Alternatively,

list(my_range(4, 16))

produces the same list.


Solution 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:

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

pure python, no imports:

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 function takes three parameters start , stop and step (third argument is optional) . range(-4,4,0.5) will solve your problem. so let's assume -x is start and x is stop.

step =  int(x/16);
range(-x, x, step);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM