小编use*_*342的帖子

如何在循环中从生成器获取三个下一个值的集合

我有一个问题,因为我无法找到解决问题的方法.

gen是一个生成器(difflib.Differ.compare()的结果):

通常通过迭代生成我可以读取每一行.问题是在每次迭代时我需要读取当前行和下两行.

示例(逐行迭代的正常输出):

iteration 1:
    line = 'a'
iteration 2:
    line = 'b'
iteration 3:
    line = 'c'
iteration 4:
    line = 'd'
iteration 5:
    line = 'e'
iteration 6:
    line = 'f'
iteration 7: 
    line = 'g'
Run Code Online (Sandbox Code Playgroud)

但在我的情况下,我需要得到这个:

iteration 1:
    line = 'a'
    next1 = 'b'
    next2 = 'c'
iteration 2:
    line = 'b'
    next1 = 'c'
    next2 = 'd'
iteration 3:
    line = 'c'
    next1 = 'd'
    next2 = 'e'
iteration 4:
    line = 'd'
    next1 = …
Run Code Online (Sandbox Code Playgroud)

python iteration generator

3
推荐指数
1
解决办法
717
查看次数

标签 统计

generator ×1

iteration ×1

python ×1