python在循环内任意递增迭代器 [英] python arbitrarily incrementing an iterator inside a loop

查看:32
本文介绍了python在循环内任意递增迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能以错误的方式处理这个问题,但我想知道如何在 python 中处理这个问题.

I am probably going about this in the wrong manner, but I was wondering how to handle this in python.

首先是一些c代码:

int i;

for(i=0;i<100;i++){
  if(i == 50)
    i = i + 10;
  printf("%i
", i);
}

好吧,所以我们永远不会看到 50 年代...

Ok so we never see the 50's...

我的问题是,我怎样才能在 python 中做类似的事情?例如:

My question is, how can I do something similar in python? For instance:

for line in cdata.split('
'):
  if exp.match(line):
    #increment the position of the iterator by 5?
    pass
  print line

由于我在 python 方面的经验有限,我只有一个解决方案,引入一个计数器和另一个 if 语句.在 exp.match(line) 为真后中断循环直到计数器达到 5.

With my limited experience in python, I only have one solution, introduce a counter and another if statement. break the loop until the counter reaches 5 after exp.match(line) is true.

必须有更好的方法来做到这一点,希望不涉及导入另一个模块.

There has got to be a better way to do this, hopefully one that does not involve importing another module.

提前致谢!

推荐答案

Python中有一个很棒的包,叫做itertools.

There is a fantastic package in Python called itertools.

但在我开始之前,先解释一下迭代协议是如何在 Python 中实现的.当你想在你的容器上提供迭代时,你指定 __iter__() 类方法,提供 迭代器类型."理解 Python 的 'for' 语句" 是一篇很好的文章,介绍了 for-in 语句实际上在 Python 中工作,并提供了关于迭代器类型如何工作的很好的概述.

But before I get into that, it'd serve well to explain how the iteration protocol is implemented in Python. When you want to provide iteration over your container, you specify the __iter__() class method that provides an iterator type. "Understanding Python's 'for' statement" is a nice article covering how the for-in statement actually works in Python and provides a nice overview on how the iterator types work.

看看以下内容:

>>> sequence = [1, 2, 3, 4, 5]
>>> iterator = sequence.__iter__()
>>> iterator.next()
1
>>> iterator.next()
2
>>> for number in iterator:
    print number 
3
4
5

现在回到 itertools.该包包含用于各种迭代目的的函数.如果您需要进行特殊排序,这是首先要研究的地方.

Now back to itertools. The package contains functions for various iteration purposes. If you ever need to do special sequencing, this is the first place to look into.

在底部,您可以找到包含 Recipes 部分使用现有的 itertools 作为构建块创建扩展工具集的秘诀.

At the bottom you can find the Recipes section that contain recipes for creating an extended toolset using the existing itertools as building blocks.

还有一个有趣的功能可以满足您的需求:

And there's an interesting function that does exactly what you need:

def consume(iterator, n):
    '''Advance the iterator n-steps ahead. If n is none, consume entirely.'''
    collections.deque(itertools.islice(iterator, n), maxlen=0)

这是一个关于其工​​作原理的快速、易读的示例(Python 2.5):

Here's a quick, readable, example on how it works (Python 2.5):

>>> import itertools, collections
>>> def consume(iterator, n):
    collections.deque(itertools.islice(iterator, n))
>>> iterator = range(1, 16).__iter__()
>>> for number in iterator:
    if (number == 5):
        # Disregard 6, 7, 8, 9 (5 doesn't get printed just as well)
        consume(iterator, 4)
    else:
        print number

1
2
3
4
10
11
12
13
14
15

这篇关于python在循环内任意递增迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆