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

遍歷列表中的所有連續(xù)項(xiàng)對(duì)

Iterate over all pairs of consecutive items in a list(遍歷列表中的所有連續(xù)項(xiàng)對(duì))
本文介紹了遍歷列表中的所有連續(xù)項(xiàng)對(duì)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

給定一個(gè)列表

l = [1, 7, 3, 5]

我想遍歷所有成對(duì)的連續(xù)列表項(xiàng)(1,7), (7,3), (3,5),即

I want to iterate over all pairs of consecutive list items (1,7), (7,3), (3,5), i.e.

for i in xrange(len(l) - 1):
    x = l[i]
    y = l[i + 1]
    # do something

我想以更緊湊的方式來做這件事,比如

I would like to do this in a more compact way, like

for x, y in someiterator(l): ...

有沒有辦法使用內(nèi)置的 Python 迭代器來做到這一點(diǎn)?我確定 itertools 模塊應(yīng)該有解決方案,但我就是想不通.

Is there a way to do do this using builtin Python iterators? I'm sure the itertools module should have a solution, but I just can't figure it out.

推薦答案

只要使用 拉鏈

>>> l = [1, 7, 3, 5]
>>> for first, second in zip(l, l[1:]):
...     print first, second
...
1 7
7 3
3 5

如果您使用 Python 2(不建議),您可能會(huì)考慮使用 itertools 中的 izip 函數(shù)來處理您不想創(chuàng)建新列表的非常長(zhǎng)的列表.

If you use Python 2 (not suggested) you might consider using the izip function in itertools for very long lists where you don't want to create a new list.

import itertools

for first, second in itertools.izip(l, l[1:]):
    ...

這篇關(guān)于遍歷列表中的所有連續(xù)項(xiàng)對(duì)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 午夜午夜精品一区二区三区文 | 亚洲精品三级 | 国产色99精品9i | 91av免费看 | 视频二区| 国产精品a久久久久 | 久久久久久久久久久久久九 | 欧美一级毛片在线播放 | 欧美日韩国产精品激情在线播放 | 欧美激情在线一区二区三区 | 国产精品欧美一区二区 | 精品国产成人 | 91麻豆精品国产91久久久资源速度 | 国产精品178页 | 亚洲一区二区三区视频在线 | 色站综合| a毛片| 久久成人国产精品 | 国产视频不卡一区 | 亚洲日韩中文字幕一区 | 自拍视频网 | 欧美一区二区 | 国产精品一二三区 | 在线观看成人精品 | 久久久精品一区 | 久在线| 国产精品一区二区精品 | 午夜精品一区二区三区在线观看 | 欧美一级大片免费看 | 国产99久久精品一区二区永久免费 | 午夜在线视频 | 免费观看视频www | 亚洲精品国产成人 | 中文字幕不卡在线观看 | 久久久久久久国产精品 | 四虎影院新网址 | 午夜私人影院在线观看 | 免费国产精品久久久久久 | 成人国产免费观看 | 99视频免费在线 | 亚洲成人国产精品 |