Skip to content Skip to sidebar Skip to footer

Change Step Value Inside Range Function?

list1 = [1, 2, 3, 4] i'm trying to figure out a way to change the step value for each printed i What I've tried r = 0 for i in range(0, 10, list1[r]): print i r = r + 1

Solution 1:

I would suggest implementing a generator of your own for this using while loop. Example -

def varied_step_range(start,stop,stepiter):
    step = iter(stepiter)
    while start < stop:
        yield start
        start += next(step)

Then you can use this as -

foriinvaried_step_range(start,stop,steplist):
    #Doyourlogic.

We do the step = iter(stepiter)so that stepiter can be any kind of iterable.


Demo -

>>>defvaried_step_range(start,stop,stepiter):...    step = iter(stepiter)...while start < stop:...yield start...        start += next(step)...>>>for i in varied_step_range(0,10,[1,2,3,4]):...print i... 
0
1
3
6

Solution 2:

Taken into consideration your comment you want something like:

>>> [range(0,10, i) for i in list1][[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8], [0, 3, 6, 9], [0, 4, 8]]

Update: Since we can't change range() step while iterating:

>> forelinlist1:
>>>    printrange(0, 10, el)

[*0*, 1, 2, 3, 4, 5, 6, 7, 8, 9][0, *2*, 4, 6, 8][0, 3, *6*, 9][0, 4, 8] (?)

There is no element from last range..

Solution 3:

You have to iterate over step, not over elements:

index = 0for i in range(len(your_list)):
    if (index+i)>=len(your_list):
        breakelse:
        print your_list[index+i]
        index = index + i

Output on list [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]:

1
2
4
7
11
16

Output on list ["a","b","c","d","e","f","g","h","i"]:

ab
d
g

Solution 4:

This is not supported by the range function. You have to do the iteration explicitly.

for elem in list1:
  i += elem
  if i > 10:
    breakprint i

Solution 5:

As questions like these seem to appear over and over again, here a even more generic solution:

classRangeBase:def__iter__(self):
        i = self.start
        direction = 1ifself.step >= 0else -1while i * direction < self.stop * direction:yield i
            i += self.step

This mixin class can be used in serveral ways:

classDynamicRange(RangeBase):
    def__init__(self, start, stop, step=1):
        self.start = start
        self.stop = stop
        self.step = step

classStepListRange(RangeBase):
    def__init__(self, start, stop, steps):
        self.start = start
        self.stop = stop
        self.steps = iter(steps)
    @propertydefstep(self):
        returnnext(self.steps)

Let's test them:

>>>a = DynamicRange(1,111,2)>>>b = iter(a)>>>next(b)
1
>>>next(b)
3
>>>next(b)
5
>>>next(b)
7
>>>next(b)
9
>>>next(b)
11
>>>next(b)
13
>>>next(b)
15
>>>a.step=3>>>next(b)
18
>>>next(b)
21
>>>next(b)
24
>>>next(b)
27
>>>next(b)
30
>>>next(b)
33
>>>next(b)
36
>>>next(b)
39
>>>next(b)
42
>>>next(b)
45
>>>a.step=30>>>next(b)
75
>>>next(b)
105
>>>next(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

and

>>>a=StepListRange(1,100,[1,2,3,4,5,6,7,8,9,10,20,30,40])>>>b=iter(a)>>>next(b)
1
>>>next(b)
3
>>>next(b)
6
>>>next(b)
10
>>>next(b)
15
>>>next(b)
21
>>>next(b)
28
>>>next(b)
36
>>>next(b)
45
>>>next(b)
55
>>>next(b)
75
>>>next(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>>

Post a Comment for "Change Step Value Inside Range Function?"