Gab*_*ell 6 python iterator lazy-evaluation
我试图实现迭代器对象的延迟分区,当迭代器的元素上的函数更改值时,该对象生成迭代器的切片.这将模仿Clojure的分区行为(虽然输出的语义会有所不同,因为Python会真正"消耗"元素).我的实现在它执行的操作数量上是最佳的,但在内存中并不是最佳的.我不明白为什么一个好的实现需要超过O(1)内存,但我的实现需要O(k)内存,其中k是分区的大小.我希望能够处理k很大的情况.有谁知道一个好的实现?
正确的行为应该是这样的
>>>unagi = [-1, 3, 4, 7, -2, 1, -3, -5]
>>> parts = partitionby(lambda x: x < 0,unagi)
>>> print [[y for y in x] for x in parts]
[[-1], [3, 4, 7], [-2], [1], [-3, -5]]
Run Code Online (Sandbox Code Playgroud)
这是我目前的版本
from itertools import *
def partitionby(f,iterable):
seq = iter(iterable)
current = next(seq)
justseen = next(seq)
partition = iter([current])
while True:
if f(current) == f(justseen):
partition = chain(partition,iter([justseen]))
try:
justseen = next(seq)
except StopIteration:
yield partition
break
else:
yield partition
current = justseen
partition = iter([])
Run Code Online (Sandbox Code Playgroud)
为什么不重复利用groupby呢?我认为是 O(1)。
def partitionby(f, iterable):
return (g[1] for g in groupby(iterable, f))
Run Code Online (Sandbox Code Playgroud)
groupby的实现与您的实现的区别在于, 是一个专门partition的迭代器对象,而不是 a chainof ...chainchain
| 归档时间: |
|
| 查看次数: |
479 次 |
| 最近记录: |