简体   繁体   English

python从特定索引继续循环

[英]python continuing a loop from a specific index

I wrote some code that uses enumerate to loop through a list. 我写了一些使用枚举遍历列表的代码。

for index, item in enumerate(list[start_idx:end_idx])
     print('index [%d] item [%s]'%(str(index), item))

the item in the list are just strings. 列表中的项目只是字符串。 Sometimes I do not want to enumerate for the whole list, sometimes I'll slice up the list do do different things. 有时我不想列举整个列表,有时我会分割列表做一些不同的事情。

The part that I am having trouble with is python's enumerate function. 我遇到麻烦的部分是python的枚举函数。

The docs say you can do: 文档说您可以做到:

for index, item in enumerate(list, start_index):
    print(str(index))

the above code doesn't do what I expected. 上面的代码没有达到我的预期。 I though enumerate would start at the start position and stop at the end, but it doesn't. 我虽然枚举会从开始位置开始并在结束时停止,但事实并非如此。

I wrote a small program and it does indeed act wonky. 我写了一个小程序,它确实表现得很奇怪。

>>> for i, x in enumerate(range(0,20),start=4):
...      print(str(i)+'\t'+str(x))
... 
4   0
5   1
6   2
7   3
8   4
9   5
10  6
11  7
12  8
13  9
14  10
15  11
16  12
17  13
18  14
19  15
20  16
21  17
22  18
23  19

I would expect enumerate to start at index 4 and loop to the end. 我希望枚举从索引4开始并循环到结尾。 So it would get the range of 4-19 but it seems to just start the index but still iterates from 0-19.. 因此它将获得4-19的范围,但似乎只是开始索引,但仍从0-19迭代。

Question, is there a way to do a iteration loop starting at a specific index in python? 问题,有没有办法从python中的特定索引开始进行迭代循环?

My expected outcome would be 我的预期结果是

>>> for i, x in enumerate(range(0,20),start=4):
...      print(str(i)+'\t'+str(x))
... 
4   0 # skip
5   1 # until
6   2 # this
7   3 # item
8   4
9   5
10  6
11  7
12  8
13  9
14  10
15  11
16  12
17  13
18  14
19  15
20  16
21  17
22  18
23  19

instead of starting the index at the start position provide. 而不是在起始位置提供索引。

Actually if you got range object it's not a big deal to make a slice from it, because range(n)[:4] is still range object( as @MosesKoledoye mentioned it's Python 3 feature ). 实际上,如果您获得了range对象,那么用它来做切片就没什么大不了的,因为range(n)[:4]仍然是range对象( 就像@MosesKoledoye提到的是Python 3功能 )。 But if you got a list, for the sake of not creating new list you can choose itertools.islice , it will return iterator. 但是,如果有列表,则为了不创建新列表,可以选择itertools.islice ,它将返回迭代器。

from itertools import islice
for index, item in enumerate(islice(range(20), 4, None)):
    print(index, item)

Output 产量

0 4
1 5
2 6
3 7
4 8
...

The start parameter of enumerate doesn't have anything to do with what elements of the iterable get selected. enumeratestart参数与选择迭代器的哪些元素没有任何关系。 It just tells enumerate what number to start with. 它只是告诉枚举以什么数字开头。

>>> list(enumerate(range(3)))
[(0, 0), (1, 1), (2, 2)]
>>> list(enumerate(range(3), 1))
[(1, 0), (2, 1), (3, 2)]

If you want to start at a specific index, you need to provide the start argument and a slice : 如果要从特定索引开始,则需要提供start参数 slice

for i, v in enumerate(alist[4:], 4):
    ...

You can do: 你可以做:

for index, item in enumerate(list[4:]):
    print(str(index))

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

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