Python list iterator behavior and next(iterator)

后端 未结 6 1008
不知归路
不知归路 2020-12-04 07:45

Consider:

>>> lst = iter([1,2,3])
>>> next(lst)
1
>>> next(lst)
2

So, advancing the iterator is, as expected, ha

6条回答
  •  情书的邮戳
    2020-12-04 08:37

    What you see is the interpreter echoing back the return value of next() in addition to i being printed each iteration:

    >>> a = iter(list(range(10)))
    >>> for i in a:
    ...    print(i)
    ...    next(a)
    ... 
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    

    So 0 is the output of print(i), 1 the return value from next(), echoed by the interactive interpreter, etc. There are just 5 iterations, each iteration resulting in 2 lines being written to the terminal.

    If you assign the output of next() things work as expected:

    >>> a = iter(list(range(10)))
    >>> for i in a:
    ...    print(i)
    ...    _ = next(a)
    ... 
    0
    2
    4
    6
    8
    

    or print extra information to differentiate the print() output from the interactive interpreter echo:

    >>> a = iter(list(range(10)))
    >>> for i in a:
    ...    print('Printing: {}'.format(i))
    ...    next(a)
    ... 
    Printing: 0
    1
    Printing: 2
    3
    Printing: 4
    5
    Printing: 6
    7
    Printing: 8
    9
    

    In other words, next() is working as expected, but because it returns the next value from the iterator, echoed by the interactive interpreter, you are led to believe that the loop has its own iterator copy somehow.

提交回复
热议问题