Home > Net >  Multiple stop and step values for dynamic range
Multiple stop and step values for dynamic range

Time:06-30

step = [0.1,0.2,0.3,0.4,0.5]

static = []
for x in step:
   range = np.arrange(5,10   x, x)
   static.append(range)
# this return a list that looks something like this [[5.,5.1,5.2,...],[5.,5.2,5.4,...],[5.,5.3,5.6,...],...]

Im trying to create standard and dynamic stop/step ranges from 5.0-10. For the standard ranges I used a list with the steps and then looped it to get the different interval lists.

What I want now is to get varying step sizes within the 5.0-10.0 interval. So for example from 5.0-7.3, the step size is 0.2, from 7.3-8.3, the range is 0.5 and then from 8.3-10.0 the lets say the step is 0.8. What I don't understand how to do is to make the dynamic run through and get all the possible combinations.

CodePudding user response:

Using a list of steps and a list of "milestones" that we are going to use to determine the start and end points of each np.arange, we can do this:

import numpy as np

def dynamic_range(milestones, steps) -> list:
    start = milestones[0]
    dynamic_range = []
    for end, step in zip(milestones[1:], steps):
        dynamic_range  = np.arange(start, end, step).tolist()
        start = end
    return dynamic_range

print(dynamic_range(milestones=(5.0, 7.3, 8.3, 10.0), steps=(0.2, 0.5, 0.8)))
# [5.0, 5.2, 5.4, 5.6, 5.8, 6.0, 6.2, 6.4, 6.6, 6.8, 7.0,
#  7.2, 7.3, 7.8, 8.3, 8.3, 9.1, 9.9]

Note on performance: this answer assumes that you are going to use a few hundred points in your dynamic range. If you want millions of points, we should try another approach with pure numpy and no list concatenation.

CodePudding user response:

if you want to be it within <5,10> interval then dont add x to 10:

import numpy as np

step = [0.1, 0.2, 0.3, 0.4, 0.5]

static = []
for x in step:
   range = np.arange(5, 10, x)
   static.append(range)

print(static)

Dinamic:

import numpy as np

step = [0.1, 0.2, 0.3, 0.4, 0.5]
breakingpoints=[6,7,8,9,10]
dinamic = []
i=0
startingPoint=5
for x in step:
    #print(breakingpoints[i])
    range = np.arange(startingPoint, breakingpoints[i], x)
    dinamic.append(range)
    i =1
    #print(range[-1])
    startingPoint=range[-1]
print(dinamic)
  • Related