python 迭代器

python 中生成器都是迭代器,而迭代器不一定是生成器。

迭代器:  满足以下两个条件

   1,有inter方法

    2,有next方法

# 首先获得Iterator对象:
# vim 9.py

it = iter([1, 2, 3, 4, 5])
while True:
    try:
        x = next(it)
        print (x)
    except StopIteration:
        break

[root@localhost python]# python 9.py
1
2
3
4
5

 

posted @ 2018-02-04 15:56  lixinliang  阅读(127)  评论(0编辑  收藏  举报