python基础知识_迭代器和生成器

前置知识

迭代:迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果。每一次对过程(函数)的重复称为一次“迭代”,而每一次迭代得到的结果(函数return)会作为下一次迭代的初始值(传入函数的参数)。
例如 斐波那契数列:
0 1 1 2 3 5 8 13 23………

F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N)

迭代器

  1. 迭代器:是一个类,有__iter__()和__next__()方法的类;
  2. 迭代器对象:是“迭代器”类的实例化对象。

备注:

  • 迭代器对象可以使用常规for语句进行遍历;
  • 字符串、列表或元组对象都可用于创建迭代器;
  • iter():类的内置函数,返回一个迭代器对象;
  • next():类的内置函数,返回下一个数据;
  • 一个类中有__iter__()方法和__next__()方法,则self也是一个迭代器对象。

实例1 自定义迭代器类

class MyNumbers:     # 因为MyNumbers类中含有__iter__()和__next__()函数,所以MyNumbers是一个迭代器
  def __iter__(self):  # 返回一个迭代器对象
    self.a = 1
    return self
 
  def __next__(self):   # 返回下一个数据
    if self.a <= 5:
      x = self.a
      self.a += 1
      return x
    else:
      raise StopIteration  # 用于标识迭代的完成,防止出现无限循环的情况,在 __next__()方法中我们可以设置在完成指定循环次数后触发 StopIteration 异常来结束迭代。
 
myclass = MyNumbers()   # myclass为迭代器对象
 
for x in myclass:   # 迭代器对象可使用for循环
  print(x)
  
  
  
>>>
1
2
3
4
5

实例2 字符串、列表、元组、字典都可用于创建迭代器。

typestr: ‘abc’list: [1,2,3]int: 1,2,3迭代器: iter([1,2,3])生成器: def g(): yeild 1dict: {‘a’:1, ‘b’:2, ‘c’:3}
可迭代的(Iterable)TrueTrueFalseTrueTrue{‘a’:1, ‘b’:2, ‘c’:3}
迭代器(Iterator)FalseFalseFalseTrueTrueFlase
# 字符串、列表、元组、字典可用于创建迭代器

# my = 'this_is_a_string'  # string
# my = [1, 2, 3, 4, 5]  # listd
# my = ('a', 'b', 'c', 'd', 'e')  # tuple
my = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}  # dictionary
it = iter(my)  # 创建迭代器对象
print(next(it))  # 输出迭代器的下一个元素
print(next(it))
print(next(it))
print(next(it))
print(next(it))


>>>
a
b
c
d
e

生成器

  • 生成器:出现了yield的函数(Python 解释器会将其视为一个 generator类);
  • 生成器通过生成器函数产生,生成器函数不用return返回,而是用yield一次返回一个结果。
  • 生成器对象:使用“生成器函数”“实例化”的对象,内部是根据“生成器类generator"创建对象,生成器内部也声明了:iter、__next__方法;

实例

#!/usr/bin/python3  
import sys  
def fibonacci(n): # 生成器函数 - 斐波那契   
	a, b, counter = 0, 1, 0    
	while True:        
		if (counter > n):            
 			return        
		yield a        
		a, b = b, a + b        
		counter += 1 
		
f = fibonacci(10) # f 是一个迭代器,由生成器返回生成  

while True:    
	try:        
		print (next(f), end=" ")    
	except StopIteration:        
		sys.exit()

可迭代对象

  • 可迭代类:有__iter__()方法的类;
  • 可迭代对象:“可迭代类”创建的对象;
  • 可迭代对象可以进行for循环;
  • 迭代器对象、生成器对象都是可迭代对象的特例。

可迭代器对象和迭代器的判断

  • 通过dir()查看是否包含以下方法:

    • 可迭代对象:包含__iter__()方法;
    • 迭代器:包含__iter__()和__next__()方法。
  • 通过Iterable 和Iterator 来判断:

typestr: ‘abc’list: [1,2,3]int: 1,2,3迭代器: iter([1,2,3])生成器: def g(): yeild 1dict: {‘a’:1, ‘b’:2, ‘c’:3}
可迭代的(Iterable)TrueTrueFalseTrueTrue{‘a’:1, ‘b’:2, ‘c’:3}
迭代器(Iterator)FalseFalseFalseTrueTrueFlase
from collections.abc import Iterable  
from collections.abc import Iterator  

# my = 'this_is_a_string'  # string
# my = [1, 2, 3, 4, 5]  # listd
# my = ('a', 'b', 'c', 'd', 'e')  # tuple
my = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}  # dictionary
it = iter(my)  # 创建迭代器对象


def f():    # 生成器函数
    yield 1


print(isinstance(it, Iterable))   # 判断是否可迭代
print(isinstance(it, Iterator))   # 判断是否是迭代器
print(isinstance(my, Iterable))
print(isinstance(my, Iterator)) 
print(isinstance(f(), Iterable))
print(isinstance(f(), Iterator))

print("it的方法:", dir(it))
print("my的方法:", dir(my))
print("f()的方法:", dir(f()))

>>>
True
True
True
False
True
True
it的方法: ['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
my的方法: ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
f()的方法: ['__class__', '__del__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__name__', '__ne__', '__new__', '__next__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'gi_code', 'gi_frame', 'gi_running', 'gi_yieldfrom', 'send', 'throw']


迭代器和生成器的区别

  • 迭代器是一个类,生成器是一个函数;
  • 生成器是一种特殊的迭代器;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值