# 迭代器
# ### 迭代器 : 能被next调用,并不断返回下一个值的对象,叫做迭代器(对象)
"""
概念:
迭代器指的是迭代取值的工具,迭代是一个重复的过程,每次重复都是基于上一次的结果而继续的,
单纯的重复并不是迭代
特征:
并不依赖索引,而通过next指针迭代所有数据,一次只取一个值,大大节省空间
"""
# 1.可迭代对象
setvar = {"a","b","c","d"}
for i in setvar:
print(i)
# dir 获取当前类型对象中的所有成员
"""__iter__ 方法用来判断是否是可迭代性数据"""
lst = dir(setvar)
print(dir("123"))
res = "__iter__" in dir(setvar)
print(res)
# 2.迭代器
"""
for 循环能够遍历一切可迭代性数据的原因在于,底层调用了迭代器,通过next方法中的指针实现数据的获取
可迭代对象 -> 迭代器 不能够被next直接调用 -> 可以被next直接调用的过程
如果是一个可迭代对象不一定是迭代器
但如果是一个迭代器就一定是一个可迭代对象
"""
# 1.如何来定义一个迭代器
setvar = {"a","b","c","d"}
it = iter(setvar)
print(it)
# 2.如何来判断一个迭代器
print(dir(it))
res = "__iter__" in dir(it) and "__next__" in dir(it)
print(res)
# 3.如何来调用一个迭代器
"""next在调用迭代器中的数据时,是单向不可逆,一条路走到黑的过程"""
res = next(it)
print(res)
res = next(it)
print(res)
res = next(it)
print(res)
res = next(it)
print(res)
"""
# StopIteration 报错 停止迭代
res = next(it)
print(res)
"""
# 4.重置迭代器
print("<===>")
it = iter(setvar)
res = next(it)
print(res)
# 5.使用其他方式判断迭代器或者可迭代对象
"""Iterator 迭代器 Iterable 可迭代对象"""
# from ... 从哪里 import 引入 ...
from collections.abc import Iterator,Iterable
res = isinstance(it,Iterator)
print(res)
res = isinstance(it,Iterable)
print(res)
# 6.使用其他方式调用迭代器中的数据
# 1. for 循环
print("<=>")
for i in it:
print(i)
# 2. for + next
print("<=>")
lst = [1,2,3,4,5,6,7,7,8,9,10]
it = iter(lst)
for i in range(8):
res = next(it)
print(res)
print(next(it))
# print(next(it))
高阶函数
### 高阶函数 : 能够把函数当成参数传递的就是高阶函数
map
"""
map(func,Iterable)
功能:
把Iterable中的数据一个一个拿出来,扔到func函数中做处理
把处理之后的结果放到迭代器当中,最后返回迭代器
参数:
func : 自定义函数 或 内置函数
Iterable : 可迭代性数据(容器类型数据 range对象 迭代器)
返回值:
迭代器
"""
# (1) lst = ["1","2","3","4"] # [1,2,3,4]
lst = ["1","2","3","4"]
# 常规写法
lst_new = []
for i in lst:
lst_new.append(int(i))
print(lst_new)
# map改造
it = map(int,lst)
"""
# 代码解析:
先把"1" 扔到int当中做处理,将强转后的结果扔到迭代器中
然后把"2" 扔到int当中做处理,将强转后的结果扔到迭代器中
然后把"3" 扔到int当中做处理,将强转后的结果扔到迭代器中
然后把"4" 扔到int当中做处理,将强转后的结果扔到迭代器中
最终返回迭代器
"""
# 获取迭代器中的数据
# 1.next
# res = next(it)
# print(res)
# res = next(it)
# print(res)
# res = next(it)
# print(res)
# res = next(it)
# print(res)
# 2.for
# for i in it:
# print(i)
# 3.for + next
# for i in range(4):
# res = next(it)
# print(res)
# 4.list强转
print(list(it))
# (2) [1,2,3,4] => [2,8,24,64]
lst = [1,2,3,4]
'''
1 * 2^1 = 2
2 * 2^2 =8
3 * 2^3 =24
4 * 2^4 =64
'''
lst_new = []
for i in lst:
res = i << i
lst_new.append(res)
print(lst_new)
# map改造
'''参数和返回值return一定要写'''
def func(n):
return n << n
lst = [1,2,3,4]
it = map(func,lst)
print(list(it))
# lambda + map
it = map(lambda n : n << n , lst)
print(list(it))
# (3) {97:"a",98:"b",99:"c"}
dic = {97:"a",98:"b",99:"c"} # ["a","b","c"] => ascii [97,98,99]
# 常规写法
dic = {"a":97,"b":98,"c":99}
def func(lst):
lst_new = []
# 遍历列表
for i in lst:
# 通过键取值
res = dic[i]
# 把值追加到新的列表中
lst_new.append(res)
# 返回新列表
return lst_new # [97,98,99]
res = func(lst = ["a","b","c"])
print(res)
# 将键值对反转
dic = {97:"a",98:"b",99:"c"}
dic_new = {}
for k,v in dic.items():
dic_new[v] = k
print(dic_new)
lst = ["a","b","c"]
lst_new = []
# 遍历列表
for i in lst:
# 通过键取值
res = dic_new[i]
# 把值追加到新的列表中
lst_new.append(res)
# 返回新列表
print(lst_new)
# map改造
def func(n):
# 原字典
dic = {97:"a",98:"b",99:"c"}
# 新字典
dic_new = {}
# 遍历原字典
for k,v in dic.items():
# 更换键值对
dic_new[v] = k
print(dic_new) # {'a': 97, 'b': 98, 'c': 99}
# 通过键来获取值
return dic_new[n]
lst = ["a","b","c"]
it = map(func,lst)
print(list(it))
filter
"""
filter(func,iterable)
功能: 过滤数据
return True 当前这个数据保留
return False 当前这个数据舍弃
参数:
func : 自定义函数
iterable : 可迭代型数据(容器类型数据,range对象,迭代器)
返回值:
迭代器
"""
lst = [1,2,3,4,5,6,7,8,9,10]
# 常规写法
lst_new = []
for i in lst:
if i % 2 == 0:
lst_new.append(i)
print(lst_new)
# filter改写
def func(i):
if i % 2 == 0:
return True
else:
return False
it = filter(func,lst)
# (1) next
res = next(it)
print(res)
# (2) for
print("<====>")
for i in it:
print(i)
# (3) for + next
it = filter(func,lst)
for i in range(3):
res = next(it)
print(res)
# (4) list强转
res = list(it)
print(res)
# filter + lambda 改写
it = filter(lambda i : True if i % 2 == 0 else False , lst )
print(list(it))