求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Modeler   模型库  
会员   
 


人工智能、机器学习& TensorFlow
6月30日-7月1日 直播



基于 UML 和EA进行分析设计
7月30-31日 北京+线上



图数据库与知识图谱
8月21日-22日 北京+线上
 
追随技术信仰

随时听讲座
每天看新闻
 
 
  Python 教程
1.Python 简介
2.Python 入门
3.Python 语法
4.Python 注释
5.Python 变量
6.Python 数据类型
7.Python 数字
8.Python Casting
9.Python 字符串
10.Python 布尔
11.Python 运算符
12.Python 列表
13.Python 元组
14.Python 集合
15.Python 字典
16.Python If ... Else
17.Python While 循环
18.Python For 循环
19.Python 函数
20.Python 数组
21.Python Lambda
22.Python Lambda
23.Python 继承
24.Python 迭代
25.Python 作用域
26.Python 模块
27.Python 日期
28.Python JSON
29.Python RegEx
30.Python PIP
31.Python Try Except
32.Python 命令行输入
33.Python 字符串格式化
Python 文件处理
1.Python 文件打开
2.Python 文件写入
3.Python 删除文件
Matplotlib教程
1.数据可视化是什么
2.Matplotlib是什么
3.Matplotlib下载和安装
4.Matplotlib.pyplot接口汇总
5.第一个Matplotlib绘图程序
6.PyLab绘制曲线图
7.Matplotlib figure图形对象
8.Matplotlib axes类使用详解
9.Matplotlib subplot()函数用法详解
10.Matplotlib subplots()函数详解
11.Matplotlib subplot2grid()函数详解
12.Matplotlib设置网格格式
13.Matplotlib设置坐标轴格式
14.Matplotlib设置坐标轴范围
15.Matplotlib设置刻度和标签
16.Matplotlib中文乱码解决方案
17.Matplotlib双轴图
18.Matplotlib设置刻度和标签
19.Matplotlib柱状图
20.Matplotlib直方图
21.Matplotlib饼状图
22.Matplotlib折线图
23.Python Matplotlib散点图
24.Matplotlib等高线图
25.Matplotlib振动图
26.Matplotlib箱型图
27.Matplotlib提琴图
28.Python Matplotlib 3D绘图详解
29.Matplotlib绘制文本
30.Matplotlib数学表达式
31.Matplotlib image图像处理
32.Matplotlib转换对象
NumPy 教程
1.NumPy是什么
2.NumPy下载与安装
3.NumPy ndarray对象
4.NumPy 简介
5.NumPy 入门
6.NumPy 数组索引
7.NumPy 数组裁切
8.NumPy 数据类型
9.NumPy 副本/视图
10.NumPy 数组形状
11.NumPy 数组重塑
12.NumPy 数组迭代
13.NumPy 数组连接
14.NumPy 数组拆分
15.NumPy 数组搜索
16.NumPy 数组排序
17.NumPy 数组过滤
18.NumPy 中的随机数
19.NumPy ufuncs
机器学习教程
1. 入门
2. 平均中位数模式
3. 标准差
4. 百分位数
5. 数据分布
6. 正态数据分布
7. 散点图
8. 线性回归
9. 多项式回归
10. 多元回归
11. 缩放
12. 训练/测试
13. 决策树
Python MySQL教程
1. Python_MySQL入门
2. Python 创建数据库
3. Python 创建表
4. Python 插入表
5. Python 从表中选取
6. Python 使用筛选器来选取
7. Python 结果排序
8. Python 删除记录
9. Python 删除表
10. Python 更新表
11. Python 限定结果
12. Python 加入
Python MongoDB
1. Python MongoDB入门
2. Python 创建数据库
3.Python MongoDB 创建集合
4.Python MongoDB 插入文档
5.Python MongoDB 查找
6.Python MongoDB 查询
7.Python MongoDB 排序
8.Python MongoDB 删除文档
9.Python 删除集合
10. Python MongoDB Update
11.Python MySQL Limit
 

 
目录
Python 迭代器
190 次浏览
2次  

Python 迭代器

迭代器是一种对象,该对象包含值的可计数数字。

迭代器是可迭代的对象,这意味着您可以遍历所有值。

从技术上讲,在 Python 中,迭代器是实现迭代器协议的对象,它包含方法 __iter__() 和 __next__() 。

迭代器 VS 可迭代对象(Iterable)

列表、元组、字典和集合都是可迭代的对象。它们是可迭代的容器,您可以从中获取迭代器(Iterator)。

所有这些对象都有用于获取迭代器的 iter() 方法:

实例

从元组返回一个迭代器,并打印每个值:

mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)

print(next(myit))
print(next(myit))
print(next(myit))

甚至连字符串都是可迭代的对象,并且可以返回迭代器:

实例

字符串也是可迭代的对象,包含一系列字符:

mystr = "banana"
myit = iter(mystr)

print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))

遍历迭代器

我们也可以使用 for 循环遍历可迭代对象:

实例

迭代元组的值:

mytuple = ("apple", "banana", "cherry")

for x in mytuple:
  print(x)
                              

实例

迭代字符串中的字符:

mystr = "banana"

for x in mystr:
  print(x)

提示: for 循环实际上创建了一个迭代器对象,并为每个循环执行 next() 方法。

创建迭代器

要把对象/类创建为迭代器,必须为对象实现 __iter__() 和 __next__() 方法。

正如您在 Python 类/对象 一章中学到的,所有类都有名为 __init__() 的函数,它允许您在创建对象时进行一些初始化。

__iter__() 方法的作用相似,您可以执行操作(初始化等),但必须始终返回迭代器对象本身。

__next__() 方法也允许您执行操作,并且必须返回序列中的下一个项目。

实例

创建一个返回数字的迭代器,从 1 开始,每个序列将增加 1(返回 1、2、3、4、5 等):

class MyNumbers:
  def __iter__(self):
    self.a = 1
    return self

  def __next__(self):
    x = self.a
    self.a += 1
    return x

myclass = MyNumbers()
myiter = iter(myclass)

print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))

StopIteration

如果你有足够的 next() 语句,或者在 for 循环中使用,则上面的例子将永远进行下去。

为了防止迭代永远进行,我们可以使用 StopIteration 语句。

在 __next__() 方法中,如果迭代完成指定的次数,我们可以添加一个终止条件来引发错误:

实例

在 20 个迭代之后停止:

class MyNumbers:
  def __iter__(self):
    self.a = 1
    return self

  def __next__(self):
    if self.a <= 20:
      x = self.a
      self.a += 1
      return x
    else:
      raise StopIteration

myclass = MyNumbers()
myiter = iter(myclass)

for x in myiter:
  print(x)

您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



190 次浏览
2次