[問題] 請問一行code .next()

看板Python作者 (嗨嗨)時間12年前 (2013/11/13 18:33), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
請問一行python code 版本是2.7 start = (n for n, e in graph.iteritems() if len(e) == 1).next() ^^^^^^ graph是一個dictionary type的object 請問後面的.next()的用途是什麼呢? 我有上網查了文件一下 他寫 Retrieve the next item from the iterator by calling its next() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised. 小弟我看不太懂 >"< 把next()拿掉也會出錯 有請好心人幫我解惑了...感恩 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.172.61.182

11/13 19:50, , 1F
(...) 內是 generator, (...).next() 回傳第一個值
11/13 19:50, 1F

11/13 19:50, , 2F
Ex. g = (x for x in [1, 2]); g.next() # 1
11/13 19:50, 2F

11/13 20:35, , 3F
樓上謝謝~~我大概了解了!
11/13 20:35, 3F
文章代碼(AID): #1IWrM0kZ (Python)
文章代碼(AID): #1IWrM0kZ (Python)