猿问

在块中迭代列表的最“pythonic”方法是什么?

在块中迭代列表的最“pythonic”方法是什么?

我有一个Python脚本,它将整数列表作为输入,我需要一次使用四个整数。不幸的是,我无法控制输入,或者我将它作为四元素元组列表传入。目前,我正在以这种方式迭代它:

for i in xrange(0, len(ints), 4):
    # dummy op for example code
    foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]

它看起来很像“C-think”,这让我怀疑有更多的pythonic方式来处理这种情况。迭代后会丢弃该列表,因此无需保留。也许这样的事情会更好吗?

while ints:
    foo += ints[0] * ints[1] + ints[2] * ints[3]
    ints[0:4] = []

但是,仍然没有“感觉”正确。: - /

相关问题:如何在Python中将列表拆分为大小均匀的块?


忽然笑
浏览 539回答 4
4回答

三国纷争

从Python的itertools文档的recipes部分修改:from itertools import zip_longestdef grouper(iterable, n, fillvalue=None):     args = [iter(iterable)] * n    return zip_longest(*args, fillvalue=fillvalue)示例在伪代码中保持示例简洁。grouper('ABCDEFG', 3, 'x') --> 'ABC' 'DEF' 'Gxx'注意:在Python 2上使用izip_longest而不是zip_longest。

吃鸡游戏

def chunker(seq, size):    return (seq[pos:pos + size] for pos in range(0, len(seq), size))# (in python 2 use xrange() instead of range() to avoid allocating a list)简单。简单。快速。适用于任何序列:text = "I am a very, very helpful text"for group in chunker(text, 7):   print repr(group),# 'I am a ' 'very, v' 'ery hel' 'pful te' 'xt'print '|'.join(chunker(text, 10))# I am a ver|y, very he|lpful textanimals = ['cat', 'dog', 'rabbit', 'duck', 'bird', 'cow', 'gnu', 'fish']for group in chunker(animals, 3):    print group# ['cat', 'dog', 'rabbit']# ['duck', 'bird', 'cow']# ['gnu', 'fish']

慕姐8265434

我是粉丝chunk_size=&nbsp;4for&nbsp;i&nbsp;in&nbsp;range(0,&nbsp;len(ints),&nbsp;chunk_size): &nbsp;&nbsp;&nbsp;&nbsp;chunk&nbsp;=&nbsp;ints[i:i+chunk_size] &nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;process&nbsp;chunk&nbsp;of&nbsp;size&nbsp;<=&nbsp;chunk_size

红糖糍粑

import&nbsp;itertoolsdef&nbsp;chunks(iterable,size): &nbsp;&nbsp;&nbsp;&nbsp;it&nbsp;=&nbsp;iter(iterable) &nbsp;&nbsp;&nbsp;&nbsp;chunk&nbsp;=&nbsp;tuple(itertools.islice(it,size)) &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;chunk: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;chunk &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chunk&nbsp;=&nbsp;tuple(itertools.islice(it,size))#&nbsp;though&nbsp;this&nbsp;will&nbsp;throw&nbsp;ValueError&nbsp;if&nbsp;the&nbsp;length&nbsp;of&nbsp;ints#&nbsp;isn't&nbsp;a&nbsp;multiple&nbsp;of&nbsp;four:for&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x1,x2,x3,x4&nbsp;in&nbsp;chunks(ints,4): &nbsp;&nbsp;&nbsp;&nbsp;foo&nbsp;+=&nbsp;x1&nbsp;+&nbsp;x2&nbsp;+&nbsp;x3&nbsp;+&nbsp;x4for&nbsp;chunk&nbsp;in&nbsp;chunks(ints,4): &nbsp;&nbsp;&nbsp;&nbsp;foo&nbsp;+=&nbsp;sum(chunk)其他方式:import&nbsp;itertoolsdef&nbsp;chunks2(iterable,size,filler=None): &nbsp;&nbsp;&nbsp;&nbsp;it&nbsp;=&nbsp;itertools.chain(iterable,itertools.repeat(filler,size-1)) &nbsp;&nbsp;&nbsp;&nbsp;chunk&nbsp;=&nbsp;tuple(itertools.islice(it,size)) &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;len(chunk)&nbsp;==&nbsp;size: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield&nbsp;chunk &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chunk&nbsp;=&nbsp;tuple(itertools.islice(it,size))#&nbsp;x2,&nbsp;x3&nbsp;and&nbsp;x4&nbsp;could&nbsp;get&nbsp;the&nbsp;value&nbsp;0&nbsp;if&nbsp;the&nbsp;length&nbsp;is&nbsp;not#&nbsp;a&nbsp;multiple&nbsp;of&nbsp;4.for&nbsp;x1,x2,x3,x4&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in&nbsp;chunks2(ints,4,0): &nbsp;&nbsp;&nbsp;&nbsp;foo&nbsp;+=&nbsp;x1&nbsp;+&nbsp;x2&nbsp;+&nbsp;x3&nbsp;+&nbsp;x4
随时随地看视频慕课网APP

相关分类

Python
我要回答