编写一个通用函数,可以迭代任何可迭代的现在返回,下一对。
def now_nxt(iterable):
iterator = iter(iterable)
nxt = iterator.__next__()
for x in iterator:
now = nxt
nxt = x
yield (now,nxt)
for i in now_nxt("hello world"):
print(i)
('h', 'e')
('e', 'l')
('l', 'l')
('l', 'o')
('o', ' ')
(' ', 'w')
('w', 'o')
('o', 'r')
('r', 'l')
('l', 'd')
我一直在考虑编写函数的最佳方法,其中可以设置每个元组中的项数。
例如,如果它是
func("hello",n=3)
结果将是:
('h','e','l')
('e','l','l')
('l','l','o')
我是使用timeit的新手,所以请指出我在这里做错了什么:
import timeit
def n1(iterable, n=1):
#now_nxt_deque
from collections import deque
deq = deque(maxlen=n)
for i in iterable:
deq.append(i)
if len(deq) == n:
yield tuple(deq)
def n2(sequence, n=2):
# now_next
from itertools import tee
iterators = tee(iter(sequence), n)
for i, iterator in enumerate(iterators):
for j in range(i):
iterator.__next__()
return zip(*iterators)
def n3(gen, n=2):
from itertools import tee, islice
gens = tee(gen, n)
gens = list(gens)
for i, gen in enumerate(gens):
gens[i] = islice(gens[i], i, None)
return zip(*gens)
def prin(func):
for x in func:
yield x
string = "Lorem ipsum tellivizzle for sure ghetto, consectetuer adipiscing elit."
print("func 1: %f" %timeit.Timer("prin(n1(string, 5))", "from __main__ import n1, string, prin").timeit(100000))
print("func 2: %f" %timeit.Timer("prin(n2(string, 5))", "from __main__ import n2, string, prin").timeit(100000))
print("func 3: %f" %timeit.Timer("prin(n3(string, 5))", "from __main__ import n3, string, prin").timeit(100000))
结果:
$ py time_this_function.py
func 1: 0.163129
func 2: 2.383288
func 3: 1.908363
答案 0 :(得分:5)
我的建议是,
from collections import deque
def now_nxt_deque(iterable, n=1):
deq = deque(maxlen=n)
for i in iterable:
deq.append(i)
if len(deq) == n:
yield tuple(deq)
for i in now_nxt_deque("hello world", 3):
print(i)
('h', 'e', 'l')
('e', 'l', 'l')
('l', 'l', 'o')
('l', 'o', ' ')
('o', ' ', 'w')
(' ', 'w', 'o')
('w', 'o', 'r')
('o', 'r', 'l')
('r', 'l', 'd')
答案 1 :(得分:5)
这是一个非常简单的方法:
itertools.tee
n
次
i
迭代器i
次izip
他们在一起import itertools
def now_next(sequence, n=2):
iterators = itertools.tee(iter(sequence), n)
for i, iterator in enumerate(iterators):
for j in range(i):
iterator.next()
return itertools.izip(*iterators)
答案 2 :(得分:2)
我的解决方案:
def nn(itr, n):
iterable = iter(itr)
last = tuple(next(iterable, None) for _ in xrange(n))
yield last
for _ in xrange(len(itr)):
last = tuple(chain(last[1:], [next(iterable)]))
yield last
这是为Python 2制作的,如果您想在Python 3中使用它,请将xrange
替换为range
。
next
,有一个很棒的default
参数,将返回而不是引发StopIteration
,您也可以将此默认参数添加到您的函数中,如下所示:
def nn(itr, n, default=None):
iterable = iter(itr)
last = tuple(next(iterable, default) for _ in xrange(n))
yield last
for _ in xrange(len(itr)):
last = tuple(chain(last[1:], [next(iterable, default)]))
yield last
我用它玩了一些,例如使用itr.__class__()
作为默认值,但对于列表和元组来说似乎是错误的,这对于字符串来说是有意义的。
答案 3 :(得分:1)
使用切片的Eric技术的变体
from itertools import tee, islice, izip
def now_next(gen, n=2):
gens = tee(gen, n)
gens = list(gens)
for i, gen in enumerate(gens):
gens[i] = islice(gens[i], i, None)
return izip(*gens)
for x in now_next((1,2,3,4,5,6,7)):
print x
答案 4 :(得分:0)
基于cravoori答案的单行:
from itertools import tee, islice, izip
def now_next(gen, n=2):
return izip(*(islice(g, i, None) for i, g in enumerate(tee(gen, n))))