开发者社区> 问答> 正文

python的迭代器是什么意思

python的迭代器是什么意思

展开
收起
云计算小粉 2018-05-10 20:11:04 1919 0
1 条回答
写回答
取消 提交回答
  • 可以直接作用于for循环的对象统称为可迭代对象(Iterable)。

    可以被next()函数调用并不断返回下一个值的对象称为迭代器(Iterator)。

    所有的Iterable均可以通过内置函数iter()来转变为Iterator。

    对迭代器来讲,有一个__next()就够了。在你使用for 和 in 语句时,程序就会自动调用即将被处理的对象的迭代器对象,然后使用它的next__()方法,直到监测到一个StopIteration异常。

    Python

    L = [1,2,3]
    [x**2 for x in L]
    [1, 4, 9]
    next(L)

    Traceback (most recent call last):
    File "", line 1, in
    TypeError: 'list' object is not an iterator

    I=iter(L)
    next(I)

    1

    next(I)

    2

    next(I)

    3

    next(I)

    Traceback (most recent call last):
    File "", line 1, in
    StopIteration
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    L = [1,2,3]
    [x**2 for x in L]

    [1, 4, 9]

    next(L)

    Traceback (most recent call last):
    File "", line 1, in
    TypeError: 'list' object is not an iterator

    I=iter(L)
    next(I)

    1

    next(I)

    2

    next(I)

    3

    next(I)

    Traceback (most recent call last):
    File "", line 1, in
    StopIteration
    上面例子中,列表L可以被for进行循环但是不能被内置函数next()用来查找下一个值,所以L是Iterable。

    L通过iter进行包装后设为I,I可以被next()用来查找下一个值,所以I是Iterator。

    题外话:

    内置函数iter()仅仅是调用了对象的__iter()方法,所以list对象内部一定存在方法iter__()
    内置函数next()仅仅是调用了对象的__next()方法,所以list对象内部一定不存在方法next__(),但是Itrator中一定存在这个方法。
    for循环内部事实上就是先调用iter()把Iterable变成Iterator在进行循环迭代的。
    Python

    L = [4,5,6]
    I = L.__iter__()
    L.__next__()

    Traceback (most recent call last):
    File "", line 1, in
    AttributeError: 'list' object has no attribute '__next__'

    I.__next__()

    4

    from collections import Iterator, Iterable
    isinstance(L, Iterable)

    True

    isinstance(L, Iterator)

    False

    isinstance(I, Iterable)

    True

    isinstance(I, Iterator)

    True

    [x**2 for x in I]

    [25, 36]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    L = [4,5,6]
    I = L.__iter__()
    L.__next__()

    Traceback (most recent call last):
    File "", line 1, in
    AttributeError: 'list' object has no attribute '__next__'

    I.__next__()

    4

    from collections import Iterator, Iterable
    isinstance(L, Iterable)

    True

    isinstance(L, Iterator)

    False

    isinstance(I, Iterable)

    True

    isinstance(I, Iterator)

    True

    [x**2 for x in I]

    [25, 36]
    4.Iterator继承自Iterable,从下面的测试中可以很方便的看到Iterator包含__iter()和next()方法,而Iteratble仅仅包含iter__()。

    Python

    from collections import Iterator, Iterable
    help(Iterator)

    Help on class Iterator:

    class Iterator(Iterable)
    | Method resolution order:
    | Iterator
    | Iterable
    | builtins.object
    |**注解:从这里可以看出Iterable继承自object, Iterator继承自Iterable。
    | Methods defined here:
    |
    | __iter__(self)
    |
    | __next__(self)
    | Return the next item from the iterator. When exhausted, raise StopIteration
    ......

    help(Iterable)

    Help on class Iterable:

    class Iterable(builtins.object)
    | Methods defined here:
    |
    | __iter__(self)
    ......
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    from collections import Iterator, Iterable
    help(Iterator)

    Help on class Iterator:

    class Iterator(Iterable)
    | Method resolution order:
    | Iterator
    | Iterable
    | builtins.object
    |**注解:从这里可以看出Iterable继承自object, Iterator继承自Iterable。
    | Methods defined here:
    |
    | __iter__(self)
    |
    | __next__(self)
    | Return the next item from the iterator. When exhausted, raise StopIteration
    ......

    help(Iterable)

    Help on class Iterable:

    class Iterable(builtins.object)
    | Methods defined here:
    |
    | __iter__(self)
    ......

    iterable需要包含有__iter()方法用来返回iterator,而iterator需要包含有next__()方法用来被循环

    如果我们自己定义迭代器,只要在类里面定义一个 iter() 函数,用它来返回一个带 next() 方法的对象就够了。

    直接上代码

    Python

    class Iterable:

    def __iter__(self):
        return Iterator()
    

    class Iterator:

    def __init__(self):
        self.start=-1
    def __next__(self):
        self.start +=2
        if self.start >10:
            raise StopIteration
        return self.start
    

    I = Iterable()
    for i in I:

    print(i)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    class Iterable:

    def __iter__(self):
        return Iterator()
    

    class Iterator:

    def __init__(self):
        self.start=-1
    def __next__(self):
        self.start +=2
        if self.start >10:
            raise StopIteration
        return self.start
    

    I = Iterable()
    for i in I:

    print(i)

    上面的代码实现的是找到10以内的奇数,代码中的类名可以随便取,不是一定需要使用我上面提供的类名的。

    如果在Iterator的__next__方法中没有实现StopIteration异常,那么则是表示的全部奇数,那么需要在调用的时候设置退出循环的条件。

    Python

    class Iterable:

    def __iter__(self):
        return Iterator()
    

    class Iterator:

    def __init__(self):
        self.start=-1
    def __next__(self):
        self.start +=2
        return self.start
    

    I = Iterable()
    for count, i in zip(range(5),I): #也可以用内置函数enumerate来实现计数工作。

    print(i)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class Iterable:

    def __iter__(self):
        return Iterator()
    

    class Iterator:

    def __init__(self):
        self.start=-1
    def __next__(self):
        self.start +=2
        return self.start
    

    I = Iterable()
    for count, i in zip(range(5),I): #也可以用内置函数enumerate来实现计数工作。

    print(i)

    我们通过range来实现打印多少个元素,这里表示打印5个元素,返回结果和上面一致。

    当然,我们可以把这两个类合并在一起,这样实现程序的简练。
    最终版本如下

    Python

    class Iterable:

    def __iter__(self):
        return self
    def __init__(self):
        self.start=-1
    def __next__(self):
        self.start +=2
        if self.start >10:
            raise StopIteration
        return self.start
    

    I = Iterable()
    for i in I:

    print(i)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class Iterable:

    def __iter__(self):
        return self
    def __init__(self):
        self.start=-1
    def __next__(self):
        self.start +=2
        if self.start >10:
            raise StopIteration
        return self.start
    

    I = Iterable()
    for i in I:

    print(i)
    

    复制迭代器

    迭代器是一次性消耗品,使用完了以后就空了,请看。

    Python

    L=[1,2,3]
    I=iter(L)
    for i in I:

    ... print(i, end='-')
    ...
    1-2-3-

    next(I)

    Traceback (most recent call last):
    File "", line 1, in
    StopIteration
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    L=[1,2,3]
    I=iter(L)
    for i in I:

    ... print(i, end='-')
    ...
    1-2-3-

    next(I)

    Traceback (most recent call last):
    File "", line 1, in
    StopIteration
    当循环以后就殆尽了,再次使用调用时会引发StopIteration异常。

    我们想通过直接赋值的形式把迭代器保存起来,可以下次使用。
    但是通过下面的范例可以看出来,根本不管用。

    Python

    I=iter(L)
    J=I
    next(I)

    1

    next(J)

    2

    next(I)

    3

    next(J)

    Traceback (most recent call last):
    File "", line 1, in
    StopIteration
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    I=iter(L)
    J=I
    next(I)

    1

    next(J)

    2

    next(I)

    3

    next(J)

    Traceback (most recent call last):
    File "", line 1, in
    StopIteration
    那怎么样才能达到我们要的效果呢?

    我们需要使用copy包中的deepcopy了,请看下面:

    Python

    import copy
    I=iter(L)
    J=copy.deepcopy(I)
    next(I)

    1

    next(I)

    2

    next(J)

    1
    1
    2
    3
    4
    5
    6
    7
    8
    9

    import copy
    I=iter(L)
    J=copy.deepcopy(I)
    next(I)

    1

    next(I)

    2

    next(J)

    1
    补充:迭代器不能向后移动, 不能回到开始。

    所以需要做一些特殊的事情才能实现向后移动等功能。

    以上代码均在Python 3.4 中测试通过。

    日志:

    8月13日完成
    8月14日添加关于Iterator, Iterable的更多解释在题外话的第4点。

    2019-07-17 22:25:07
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载