迭代器,迭代和迭代究竟是什么? [英] What exactly are iterator, iterable, and iteration?

查看:171
本文介绍了迭代器,迭代和迭代究竟是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

迭代,迭代器和Python中的迭代的最基本定义是什么?

What are the most basic definitions of "iterable", "iterator" and "iteration in Python?

我读过多个定义,但它们的确切含义仍然赢了不会陷入其中。

I've read multiple definitions but their exact meaning still won't sink in.

有人可以帮助我解决这个基本想法吗?

Can someone please help me with the basic idea?

推荐答案

迭代是一个接下来的每个项目的通用术语。任何时候你使用循环,显式或隐式,来回顾一组项目,即迭代。

Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration.

在Python中,可迭代迭代器具有特定含义。

In Python, iterable and iterator have specific meanings.

iterable 是一个对象,它有一个 __ iter __ 方法,它返回迭代器,或者定义一个 __ getitem __ 方法,它可以从零开始连续索引(并在索引不再有效时引发 IndexError )所以 iterable 是一个你可以获得的对象迭代器来自。

An iterable is an object that has an __iter__ method which returns an iterator, or which defines a __getitem__ method that can take sequential indexes starting from zero (and raises an IndexError when the indexes are no longer valid). So an iterable is an object that you can get an iterator from.

迭代器是一个 next (Python 2)或 __ next __ (Python 3)方法。

An iterator is an object with a next (Python 2) or __next__ (Python 3) method.

每当你使用进行循环,或者 map 或者在Python中使用列表推导等,自动调用 next 方法从迭代器中获取每个项目,从而完成整个过程迭代

Whenever you use a for loop, or map, or a list comprehension, etc. in Python, the next method is called automatically to get each item from the iterator, thus going through the process of iteration.

开始学习的好地方是iteite部分迭代器标准类型页面的类型部分。了解基础知识后,请尝试使用Functional Programming HOWTO的迭代器部分

A good place to start learning would be the iterators section of the tutorial and the iterator types section of the standard types page. After you understand the basics, try the iterators section of the Functional Programming HOWTO.

这篇关于迭代器,迭代和迭代究竟是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆