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

查看:112
本文介绍了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\n", 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('\n'):
  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-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天全站免登陆