我嘗試使用過字典嵌套發生器理解與列表作爲存儲的值,並觀察到以下怪(我的)行爲:誤區關於嵌套產生理解上的字典
Python 2.6.5 (r265:79063, Oct 1 2012, 22:07:21)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dummy = {1:[1,2,3],2:[2,3,4],3:[3,4,5]}
>>> a = (d for _,d in dummy.iteritems())
>>> a.next()
[1, 2, 3]
>>> a.next()
[2, 3, 4]
>>> a.next()
[3, 4, 5]
>>> a.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
這是有道理的。接下來沒有(至少對我來說)
>>> aa = (dd for dd in (d for _,d in dummy.iteritems()))
>>> aa.next()
[1, 2, 3]
>>> aa.next()
[2, 3, 4]
>>> aa.next()
[3, 4, 5]
>>> aa.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
爲什麼我的(未遂)嵌套發生器理解的行爲方式作爲非嵌套的版本一樣嗎?我會期望每個aa.next()都給出一個單獨的元素結果,而不是一個列表。