python初学一(迭代器)

一、迭代器的基本知识:

      1、迭代器的创建:

              (1)系统函数iter()

              (2)迭代对象内部函数   .__iter__()

str1 = 'abcdefg'
aaa = str1.__iter__()       等同于aaa = iter(str1)
print(aaa)           # <str_iterator object at 0x0000020E1CCEB048>

      2、迭代器与生成器的区别:

                 生成器是由函数 + yield 创造的,生成器本身就是迭代器,他是有个返回值是迭代器的函数。

                 迭代器是由可迭代对象通过iter()或.__iter__()创建的。

      3、迭代器的遍历:

              (1)系统函数next()方法:

              (2)迭代对象内部函数  . __next__()方法

              (3)for循环遍历。

二、

猜你喜欢

转载自blog.csdn.net/qq_16555103/article/details/83829959