迭代序列的 Pythonic 方式,一次 4 个项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3415072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:56:33  来源:igfitidea点击:

Pythonic way to iterate over sequence, 4 items at a time

pythoniteration

提问by blasted

Possible Duplicate:
What is the most “pythonic” way to iterate over a list in chunks?

可能的重复:
在块中迭代列表的最“pythonic”方式是什么?

I am reading in some PNG data, which has 4 channels per pixel. I would like to iterate over the data 1 pixel at a time (meaning every 4 elements = 1 pixel, rgba).

我正在读取一些 PNG 数据,每个像素有 4 个通道。我想一次迭代 1 个像素的数据(意思是每 4 个元素 = 1 个像素,rgba)。

red_channel = 0
while red_channel < len(raw_png_data):
    green_channel, blue_channel, alpha_channel = red_channel +1, red_channel +2, red_channel +3
    # do something with my 4 channels of pixel data ... raw_png_data[red_channel] etc
    red_channel += 4

This way doesnt really seem "right". Is there a more Pythonic way to iterate over a sequence, 4 items at a time, and have those 4 items unpacked?

这种方式似乎并不“正确”。是否有一种更 Pythonic 的方式来迭代一个序列,一次 4 个项目,然后将这 4 个项目解包?

回答by excid3

Try something like this:

尝试这样的事情:

for red, green, blue, alpha in raw_png_data:
    #do something

You can pull out multiple items and never have to use an iterator. :)

您可以提取多个项目,而不必使用迭代器。:)

Edit: This would mean that raw_png_data needs to be a list of 4 value tuples. It would be most pythonic to put each rgba group into a tuple and then append it to raw_png_data and iterate through like my example.

编辑:这意味着 raw_png_data 需要是 4 个值元组的列表。将每个 rgba 组放入一个元组中,然后将其附加到 raw_png_data 并像我的示例一样迭代将是最pythonic的。

回答by Tomasz Wysocki

vars = [1, 2, 3, 4, 5, 6, 7, 8]
for a, b, c, d in zip(*[iter(vars)]*4):
    print a, b, c, d

回答by Sjoerd

for r, g, b, t in (data[i:i+4] for i in xrange(0, len(data)/4*4, 4)):
    print r, g, b, t

回答by kennytm

(Python's itertools should really make all recipesas standard functions...)

(Python 的 itertools 应该真正将所有配方作为标准功能......)

You could use the grouperfunction:

您可以使用该grouper功能:

from itertools import zip_longest
def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

Then you can iterate the pixels by

然后你可以迭代像素

for r,g,b,a in grouper(4, raw_png_data):
  ....

Alternatively, you could use

或者,您可以使用

irpd = iter(raw_png_data)
for r,g,b,a in zip(irpd, irpd, irpd, irpd):  # use itertools.izip in Python 2.x
  ....

Note that this will chop the last few bytes if the iterable's length is not a multiple of 4. OTOH, the grouperfunction uses izip_longest, so the extra bytes will be padded with None for that.

请注意,如果迭代的长度不是 4 的倍数,这将截断最后几个字节。 OTOH,该grouper函数使用izip_longest,因此额外的字节将用 None 填充。

回答by John La Rooy

from itertools import izip
for r,g,b,a in izip(*[iter(data)]*4):
    ...