import itertools
with open(fn) as f:
for line, line2 in itertools.izip_longest(f, f, fillvalue=''):
print line, line2
Alas, izip_longest
requires Python 2.6 or better; 2.5 only has izip
, which would truncate the last line if f
has an odd number of lines. It's quite easy to supply the equivalent functionality as a generator, of course.
Here's a more general "N at a time" iterator-wrapper:
def natatime(itr, fillvalue=None, n=2):
return itertools.izip_longest(*(iter(itr),)*n, fillvalue=fillvalue)
itertools
is generally the best way to go, but, if you insisted on implementing it by yourself, then:
def natatime_no_itertools(itr, fillvalue=None, n=2):
x = iter(itr)
for item in x:
yield (item,) + tuple(next(x, fillvalue) for _ in xrange(n-1))
In 2.5, I think the best approach is actually not a generator, but another itertools-based solution:
def natatime_25(itr, fillvalue=None, n=2):
x = itertools.chain(iter(itr), (fillvalue,) * (n-1))
return itertools.izip(*(x,)*n)
(since 2.5 doesn't have the built-in next
, as well as missing izip_longest
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…