作者tainanyes (嗨嗨)
看板Python
标题[问题] 请问一行code .next()
时间Wed Nov 13 18:33:34 2013
请问一行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
1F:推 ccwang002:(...) 内是 generator, (...).next() 回传第一个值 11/13 19:50
2F:→ ccwang002:Ex. g = (x for x in [1, 2]); g.next() # 1 11/13 19:50
3F:→ tainanyes:楼上谢谢~~我大概了解了! 11/13 20:35