unwind 的答案适用于小文件。如果您想要一个通用的解决方案,请坚持使用迭代器和生成器。
你可以定义一个生成器函数来生成line_i、line_i+3和line_i+4的元组。
随着进口
from collections import deque
from itertools import islice
我们可以定义一个新的生成器函数:
def reflexive_zip(iterator, offset = 0):
offset = int(offset)
if offset == 0:
for element in iterator:
yield (element, element)
else:
d = deque(islice(iterator,abs(offset)))
for element in iterator:
d.append(element)
if offset < 0:
yield (element, d.popleft())
else:
yield (d.popleft(), element)
它需要 a iterator
,使用 a 创建一些缓冲区deque
并产生 的元素的元组iterator
。偏移量可以通过offset
参数控制。
完整的脚本来了。用法示例在主要部分。
if __name__ == "__main__":
from cStringIO import StringIO
f = StringIO("""Header 1
line 1
line 2
line 3
line 4
Header 2
line 1
line 2
line 3
line 4""")
#for line, other_line in reflexive_zip(f, 4):
# print "%s -> %s" %(line, other_line)
for ((line, _ignore),(line3, line4)) in reflexive_zip(reflexive_zip(f,1),3):
print "%s -> %s %s" %(line, line3, line4)
from collections import deque
from itertools import islice
def reflexive_zip(iterator, offset = 0):
offset = int(offset)
if offset == 0:
for element in iterator:
yield (element, element)
else:
d = deque(islice(iterator,abs(offset)))
for element in iterator:
d.append(element)
if offset < 0:
yield (element, d.popleft())
else:
yield (d.popleft(), element)
if __name__ == "__main__":
from cStringIO import StringIO
f = StringIO("""Header 1
line 1
line 2
line 3
line 4
Header 2
line 1
line 2
line 3
line 4""")
#for line, other_line in reflexive_zip(f, 4):
# print "%s -> %s" %(line, other_line)
for ((line, _ignore),(line3, line4)) in reflexive_zip(reflexive_zip(f,1),3):
print "%s -> %s %s" %(line, line3, line4)
输出:
Header 1
-> line 3
line 4
line 1
-> line 4
Header 2
line 2
-> Header 2
line 1
line 3
-> line 1
line 2
line 4
-> line 2
line 3
Header 2
-> line 3
line 4