简体   繁体   English

更改范围函数内的步长值?

[英]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

I would suggest implementing a generator of your own for this using while loop.我建议为此使用while循环实现您自己的生成器。 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.我们执行step = iter(stepiter)以便stepiter可以是任何类型的可迭代对象。


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

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:更新:由于我们无法在迭代时更改range()步骤:

>> 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..最后一个范围没有元素..

This is not supported by the range function. range 函数不支持此操作。 You have to do the iteration explicitly.您必须明确地进行迭代。

for elem in list1:
  i += elem
  if i > 10:
    break
  print i

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,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","c","d","e","f","g","h","i"]

a
b
d
g

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:这个 mixin 类可以以多种方式使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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