iterable and iterator in Python

Posted 除了心跳都忘掉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iterable and iterator in Python相关的知识,希望对你有一定的参考价值。

Summary

Everything you can loop over is an iterable. Looping over iterables works via getting an iterator from an iterable and then repeatedly asking the iterator for the next item.

The way iterators and iterables work is called the iterator protocol. List comprehensions, tuple unpacking, for loops, and all other forms of iteration rely on the iterator protocol.


Iterable

  1. In the Python world, an iterable is any object that you can loop over with a for loop
  2. Iterables are not always indexable, they don’t always have lengths, and they’re not always finite.;

Iterable & Iterator

  1. All iterables can be passed to the built-in iter function to get an iterator from them;
  2. Iterators have exactly one job: return the “next” item in our iterable;
  3. So iterators can be passed to the built-in next function to get the next item from them and if there is no next item (because we reached the end), a StopIteration exception will be raised;

The Iterator Protocol

The iterator protocol is a fancy term meaning “how iterables actually work in Python”.

Let’s redefine iterables from Python’s perspective.

Iterable:

1.Can be passed to the iter function to get an iterator for them
2.There is no 2. That’s really all that’s needed to be an iterable

Iterator:

1.Can be passed to the next function which gives their next item or raises StopIteration
2.Return themselves when passed to the iter function

The inverse of these statements should also hold true. Which means:

1.Anything that can be passed to iter without an error is an iterable.
2.Anything that can be passed to next without an error (except for StopIteration) is an iterator
3.Anything that returns itself when passed to iter is an iterator


Looping with iterators

This while loop manually loops over some iterable, printing out each item as it goes:

def print_each(iterable):
    iterator = iter(iterable)
    while True:
        try:
            item = next(iterator)
        except StopIteration:
            break
        else:
            print(item)

We can call this function with any iterable and it will loop over it:

>>> print_each(1, 2, 3)
1
2
3

The above function is essentially the same as this one which uses a for loop:

def print_each(iterable):
    for item in iterable:
        print(item)

This for loop is automatically doing what we were doing manually: calling iter to get an iterator and then calling next over and over until a StopIteration exception is raised.

Using the iterator protocol (either manually or automatically) is the only universal way to loop over any iterable in Python.

以上是关于iterable and iterator in Python的主要内容,如果未能解决你的问题,请参考以下文章

[Chapter 2] Value Iteration and Policy Iteration

Iterator and Generator

Python Iterator and Generator

Python "for loop and def" 练习并得到 "TypeError: 'int' object is not iterable"

人工智能之计算最佳策略(Policy Iteration and Value Iteration)

Mutation and Iteration