geo*_*org 4 python generator python-itertools
我有一个类似于itertools pairwise
配方的生成器,它产生了(s0,s1), (s1,s2), (s2, s3)...
.我想从它创建另一个生成器,它将产生原始序列s0, s1, s2, s3,...
.
from itertools import *
def pairwise(iterable):
a, b = tee(iterable)
next(b, None)
return izip(a, b)
a = [1,2,3,4]
for item in unpair(pairwise(a)):
print item # should print 1,2,3,4
Run Code Online (Sandbox Code Playgroud)
如何在unpair
不诉诸列表的情况下编写为生成器?
也许:
def unpair(iterable):
p = iter(iterable)
return chain(next(p, []), (x[1] for x in p))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
179 次 |
最近记录: |