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

如何并行遍歷兩個列表?

How to iterate through two lists in parallel?(如何并行遍歷兩個列表?)
本文介紹了如何并行遍歷兩個列表?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在 Python 中有兩個可迭代對象,我想成對檢查它們:

I have two iterables in Python, and I want to go over them in pairs:

foo = (1, 2, 3)
bar = (4, 5, 6)

for (f, b) in some_iterator(foo, bar):
    print("f: ", f, "; b: ", b)

它應(yīng)該導(dǎo)致:

f: 1; b: 4
f: 2; b: 5
f: 3; b: 6

一種方法是迭代索引:

for i in range(len(foo)):
    print("f: ", foo[i], "; b: ", bar[i])

但這對我來說似乎有些不合時宜.有沒有更好的方法?

But that seems somewhat unpythonic to me. Is there a better way to do it?

推薦答案

Python 3

for f, b in zip(foo, bar):
    print(f, b)

zipfoobar 中較短者停止時停止.

zip stops when the shorter of foo or bar stops.

Python 3 中,zip返回元組的迭代器,如 Python2 中的 itertools.izip.獲取列表元組,使用 list(zip(foo, bar)).并壓縮直到兩個迭代器都筋疲力盡,你會用itertools.zip_longest.

In Python 3, zip returns an iterator of tuples, like itertools.izip in Python2. To get a list of tuples, use list(zip(foo, bar)). And to zip until both iterators are exhausted, you would use itertools.zip_longest.

Python 2 中,zip返回一個元組列表.當(dāng) foobar 不是很大時,這很好.如果它們都是巨大的,那么形成 zip(foo,bar) 是不必要的巨大臨時變量,應(yīng)替換為 itertools.izipitertools.izip_longest,它返回一個迭代器而不是一個列表.

In Python 2, zip returns a list of tuples. This is fine when foo and bar are not massive. If they are both massive then forming zip(foo,bar) is an unnecessarily massive temporary variable, and should be replaced by itertools.izip or itertools.izip_longest, which returns an iterator instead of a list.

import itertools
for f,b in itertools.izip(foo,bar):
    print(f,b)
for f,b in itertools.izip_longest(foo,bar):
    print(f,b)

izipfoobar 用盡時停止.當(dāng) foobar 都用盡時,izip_longest 停止.當(dāng)較短的迭代器用盡時,izip_longest 會產(chǎn)生一個元組,其中 None 在對應(yīng)于該迭代器的位置.您還可以根據(jù)需要設(shè)置除 None 之外的其他 fillvalue.查看全文.

izip stops when either foo or bar is exhausted. izip_longest stops when both foo and bar are exhausted. When the shorter iterator(s) are exhausted, izip_longest yields a tuple with None in the position corresponding to that iterator. You can also set a different fillvalue besides None if you wish. See here for the full story.

還要注意 zip 及其類似 zip 的 brethen 可以接受任意數(shù)量的可迭代對象作為參數(shù).例如,

Note also that zip and its zip-like brethen can accept an arbitrary number of iterables as arguments. For example,

for num, cheese, color in zip([1,2,3], ['manchego', 'stilton', 'brie'], 
                              ['red', 'blue', 'green']):
    print('{} {} {}'.format(num, color, cheese))

打印

1 red manchego
2 blue stilton
3 green brie

這篇關(guān)于如何并行遍歷兩個列表?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 九九福利 | 色视频在线播放 | 欧美精品电影一区 | 国产精品视频一区二区三 | 国产一区二区免费 | 亚洲精品无 | 欧美一区二区三 | m豆传媒在线链接观看 | 欧美韩一区二区 | 狠狠婷婷综合久久久久久妖精 | 午夜免费视频 | 国产成人精品网站 | 仙人掌旅馆在线观看 | 国产精品中文字幕在线观看 | av夜夜操| 一级毛片视频在线 | 成人精品在线观看 | 色视频网站 | 欧美色综合一区二区三区 | 成人精品一区二区三区中文字幕 | 91久久北条麻妃一区二区三区 | 黄色一级大片视频 | 精品一二区 | 午夜成人在线视频 | 伊人狠狠干 | 欧美人妖网站 | 国产精品美女久久久久aⅴ国产馆 | 久久久久国产一区二区三区四区 | 久久精品91久久久久久再现 | 一区二区三区四区在线 | 粉色午夜视频 | av黄色在线观看 | 99精品视频免费观看 | 九九久久精品 | 国产精品免费大片 | 久久综合伊人 | 久久黄色精品视频 | 黄色片免费在线观看 | 亚洲天堂色| 欧美精品一区二区三区在线 | 久久www免费人成看片高清 |