3

我有一个问题,因为我找不到我的问题的解决方案。

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

通常通过迭代 gen 我可以读取每一行。问题是,在每次迭代中,我都需要读取当前行和下两行。

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

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'

但就我而言,我需要得到这个:

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 = 'e'
    next2 = 'f'
iteration 5:
    line = 'e'
    next1 = 'f'
    next2 = 'g'
iteration 6:
    line = 'f'
    next1 = 'g'
    next2 = None
iteration 7: 
    line = 'g'
    next1 = None
    next2 = None

我试图玩 gen.send()、itertools.islice(),但我找不到合适的解决方案。我不想将此生成器转换为列表(然后我可以将 next1 读取为 gen[i + 1],将 next2 读取为 gen[i + 2],但是当 diff 输出很大时,这完全是低效的。

4

5 回答 5

6

这是我建议的任何迭代器/生成器的通用解决方案。我认为这种方式最有效。

def genby3(gen):
    it = iter(gen) # Make it a separate iterator, to avoid consuming it totally
    L1 = it.next() # Get the first two elements
    L2 = it.next()
    for L3 in it:
        yield [L1, L2, L3] # Get the results grouped in 3
        L1, L2 = L2, L3 # Update the last 2 elements
    yield [L2, L3, None] # And take care of the last 2 cases
    yield [L3, None, None]

print list(genby3(xrange(10)))

如果它是您正在读取的文件,您可以seek,readline然后返回,但它可能会变得混乱,因此您可以将其视为任何其他迭代器。

更新:使它在每次迭代中不仅仅适用于 3 个项目,它的工作原理与其他项目一样。

def genby(gen, n):
    assert n>=1, 'This does not make sense with less than one element'
    it = iter(gen)
    last = list(it.next() for i in xrange(n-1))

    for nth_item in it:
        last = last+[nth_item]
        yield last
        last.pop(0)

    for i in xrange(n-1):
        last = last+[None]
        yield last
        last.pop(0)

r = xrange(10)
for i, n in enumerate(genby(r, 3)):
    print i, 'iteration'
    print '\t', n

编辑 2:在 yield 语句之前移动了列表的连接,只是为了避免重复两次。稍微提高性能明智。

于 2012-12-05T21:03:48.290 回答
3

尝试保留临时变量。

line = iterator.next()
next1 = iterator.next()

for next2 in iterator:
    #do stuff
    line = next1
    next1 = next2
于 2012-12-05T20:55:40.920 回答
2

文档中有一个食谱itertoolspairwise()。它可以适应:

from itertools import tee, izip_longest

def triplewise(iterable):
    xs, ys, zs = tee(iterable, 3)
    next(ys, None)
    next(zs, None)
    next(zs, None)
    return izip_longest(xs, ys, zs)

for line, next1, next2 in triplewise(gen):
    ...

也可以概括为:

from itertools import tee, izip, izip_longest, islice

no_fillvalue = object()

def nwise(iterable, n=2, fillvalue=no_fillvalue):
    iters = (islice(each, i, None) for i, each in enumerate(tee(iterable, n)))
    if fillvalue is no_fillvalue:
        return izip(*iters)
    return izip_longest(*iters, fillvalue=fillvalue)

for line, next1, next2 in nwise(gen, 3, None):
    ...
于 2012-12-05T21:09:44.307 回答
1

将三个序列压缩在一起怎么样?

izip_longest(gen, islice(gen,1,None), islice(gen,2,None), fillvalue=None)
于 2012-12-05T20:55:52.047 回答
1

你可以使用这样的东西:

def genTriplets(a):
    first = a.next()
    second = a.next()
    third = a.next()
    while True:
        yield (first, second, third)
        first = second
        second = third
        try:
            third = a.next()
        except StopIteration:
            third = None
            if (first is None and second is None and third is None):
                break
于 2012-12-05T21:12:24.653 回答