2
我很困惑tee()
是如何工作的。itertools tee()迭代器拆分
l = [1, 2, 3, 4, 5, 6]
iterators3 = itertools.tee(l, 3)
for i in iterators3:
print (list(i))
輸出:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
這是確定。但是,如果我嘗試:
a, b, c = itertools.tee(l)
我得到這個錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)
爲什麼?
你的3在哪裏? –
@MosesKoledoye是的,我現在明白了。 – MishaVacic
@Coldspeed'tee'確實不**返回一個生成器對象,即使它已經生成,你仍然可以解開一個生成器,試試'head,* rest =(我在範圍內(10)),因爲發電機是可迭代的... –