手动遍历迭代器-python cookbook(第3版)高清中文完整版

时间:2021-06-10 05:19:21
【文件属性】:
文件名称:手动遍历迭代器-python cookbook(第3版)高清中文完整版
文件大小:4.84MB
文件格式:PDF
更新时间:2021-06-10 05:19:21
python cookbook 第3版 高清 中文完整版 4.1 手动遍历迭代器 问题 你想遍历一个可迭代对象中的所有元素,但是却不想使用for循环。 解决方案 为了手动的遍历可迭代对象,使用 next() 函数并在代码中捕获 StopIteration 异常。 比 如,下面的例子手动读取一个文件中的所有行: def manual_iter(): with open('/etc/passwd') as f: try: while True: line = next(f) print(line, end='') except StopIteration: pass

网友评论