izip
은 꽤 좋은 옵션이지만, 여기 당신이 그것으로 불행에 있기 때문에 몇 가지 대안을 : 당신이 볼 수 있듯이
>>> def chunker(seq, size):
... return (tuple(seq[pos:pos+size]) for pos in xrange(0, len(seq), size))
...
>>> x = range(11)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> chunker(x, 2)
<generator object <genexpr> at 0x00B44328>
>>> list(chunker(x, 2))
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10,)]
>>> list(izip(x[1::2], x[::2]))
[(1, 0), (3, 2), (5, 4), (7, 6), (9, 8)]
가이 제대로 요소의 고르지 못한 양을 처리 할 수있는 장점이있는 당신에게 중요 할 수도 있고 중요하지 않을 수도 있습니다. itertools documentation itself에서이 조리법도 있습니다 :
>>> def grouper(n, iterable, fillvalue=None):
... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
... args = [iter(iterable)] * n
... return izip_longest(fillvalue=fillvalue, *args)
...
>>>
>>> from itertools import izip_longest
>>> list(grouper(2, x))
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, None)]
O는 정말이 솔루션을 좋아한다. – uolot
아하, 왜 코멘트를 편집 할 수 없습니까? "나는 정말로 ..."해야합니다. – uolot