<blockquote>
<p><strong>Possible Duplicate:</strong><br/>
<a href="https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks">What is the most “pythonic” way to iterate over a list in chunks?</a> </p>
</blockquote>
<p>我正在读取一些PNG数据,每像素有4个通道。我想一次迭代数据1个像素(意味着每4个元素=1个像素,rgba)。</p>
<pre><code>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
</code></pre>
<p>这种方式似乎并不“正确”。有没有一种更像python的方法来迭代一个序列,一次4个条目,并将这4个条目解包?</p>