python 迭代器
from collections import Iterablefrom collections import Iterator‘‘‘可迭代对象:可以直接作用于for循环的对象统称为可迭代对象(Iterable)。可以用isinstance()去判断一个对象是否是Iterable对象可以直接作用于for的数据类型一般分两种1、集合数据类型,如list、tuple、dict、set、string2、是generator,包括生成器和带yield的generator function‘‘‘print(isinstance([], Iterable))print(isinstance((), Iterable))print(isinstance({}, Iterable))print(isinstance("", Iterable))print(isinstance((x for x in range(10)), Iterable))print(isinstance(1, Iterable))‘‘‘迭代器:不但可以作用于for循环,还可以被next()函数不断调用并返回下一个值,直到最后跑出一个StopIteration错误表示无法继续返回下一个值可以被next()函数调用并不断返回下一个值的对象称为迭代器(Iterator对象)可以使用isinstance()函数判断一个对象是否是Iterator对象‘‘‘print(isinstance([], Iterator))print(isinstance((), Iterator))print(isinstance({}, Iterator))print(isinstance("", Iterator))print(isinstance((x for x in range(10)), Iterator))l = (x for x in [23,4,5,64,3435])print(next(l))print(next(l))print(next(l))print(next(l))print(next(l))#print(next(l))#转成Iterator对象a = iter([1,2,3,4,5])print(next(a))print(next(a))print(isinstance(iter([]), Iterator))print(isinstance(iter(()), Iterator))print(isinstance(iter({}), Iterator))print(isinstance(iter(‘‘), Iterator))
相关推荐
Andrewjdw 2020-07-26
czsay 2020-06-01
JnX 2020-09-21
joyjoy0 2020-09-18
RocNg 2020-04-18
assastor 2020-03-20
AndroidAmelia 2020-03-01
kikaylee 2020-01-20
singer 2019-12-29
Jan 2020-08-17
shenxiuwen 2020-08-01
fanhuasijin 2020-06-21
丽丽 2020-06-08
容数据服务集结号 2020-06-08
程序员之怒 2020-04-26
wmsjlihuan 2020-04-26
oDongTianShuiYue 2020-04-26
kong000dao0 2020-04-29