Change step value inside range function?

前端 未结 5 1159
醉话见心
醉话见心 2020-12-21 19:58
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         


        
相关标签:
5条回答
  • 2020-12-21 20:06

    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:

    >> for el in list1:
    >>>    print range(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..

    0 讨论(0)
  • 2020-12-21 20:06

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

    class RangeBase:
        def __iter__(self):
            i = self.start
            direction = 1 if self.step >= 0 else -1
            while i * direction < self.stop * direction:
                yield i
                i += self.step
    

    This mixin class can be used in serveral ways:

    class DynamicRange(RangeBase):
        def __init__(self, start, stop, step=1):
            self.start = start
            self.stop = stop
            self.step = step
    
    class StepListRange(RangeBase):
        def __init__(self, start, stop, steps):
            self.start = start
            self.stop = stop
            self.steps = iter(steps)
        @property
        def step(self):
            return next(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
    >>>
    
    0 讨论(0)
  • 2020-12-21 20:13

    You have to iterate over step, not over elements:

    index = 0
    for i in range(len(your_list)):
        if (index+i)>=len(your_list):
            break
        else:
            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"]:

    a
    b
    d
    g
    
    0 讨论(0)
  • 2020-12-21 20:22

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

    for elem in list1:
      i += elem
      if i > 10:
        break
      print i
    
    0 讨论(0)
  • 2020-12-21 20:28

    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 -

    for i in varied_step_range(start,stop,steplist):
        #Do your logic.
    

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


    Demo -

    >>> def varied_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
    
    0 讨论(0)
提交回复
热议问题