久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

python在循環內任意遞增迭代器

python arbitrarily incrementing an iterator inside a loop(python在循環內任意遞增迭代器)
本文介紹了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在循環內任意遞增迭代器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 中文字幕在线一区 | 人人看人人爽 | 欧美日韩久久 | 91福利在线观看视频 | 日本成年免费网站 | 欧美8一10sex性hd | 久久精品久久久久久 | 欧美成人高清视频 | 国产黄色大片网站 | 九九久久精品 | 欧美一区二区三区精品免费 | 免费日韩网站 | 成人免费看片 | 欧美日韩精品一区 | 国产成人精品一区二区三区 | 国产精品久久毛片av大全日韩 | 欧洲精品一区 | 亚洲精品在线国产 | 2022精品国偷自产免费观看 | 国产一区www | 91视频网 | 91免费在线播放 | 国产日产久久高清欧美一区 | av黄色在线观看 | 日韩欧美在线免费观看视频 | 九九热在线免费视频 | 国产在线观看一区 | 午夜久久久久久久久久一区二区 | 欧美成人在线免费 | hsck成人网| 999久久久久久久久6666 | 日本aa毛片a级毛片免费观看 | 久久噜噜噜精品国产亚洲综合 | 91不卡| 毛片一区二区 | 国产精品国产三级国产aⅴ无密码 | 欧美一级视频免费看 | 成人国产免费观看 | 亚洲精品免费视频 | 久久亚洲一区二区 | 国产视频精品区 |