一.迭代器
能被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)
...
StopIteration 报错 停止迭代
4.重置迭代器
it = iter(setvar)
res = next(it)
print(res)
5.使用其他方式判断迭代器或者可迭代对象
Iterator 迭代器 Iterable 可迭代对象
# from ... 从哪里 import 引入 ...
from collections import Iterator,Iterable
# from collections.abc import Iterator,Iterable python3.7
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(10):
res = next(it)
print(res)
print(next(it))
print(next(it))
一.高阶函数 : 能够把函数当成参数传递的就是高阶函数
1.map
map(func,Iterable)
功能:处理数据
把Iterable中的数据一个一个拿出来,扔到func函数中做处理
把处理之后的结果放到迭代器当中,最后返回迭代器
参数:
func : 自定义函数 或 内置函数
Iterable : 可迭代性数据(容器类型数据 range对象 迭代器)
返回值:
迭代器
(1) lst = [“1”,“2”,“3”,“4”] # [1,2,3,4]
it = map(int,lst)
获取迭代器中的数据
# 1.next
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”}
# 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) # map函数吧lst打散传入func处理,返回处理后的结果
print(list(it))
2.filter
filter(func,iterable)
功能: 过滤数据
return True 当前这个数据保留
return False 当前这个数据舍弃
参数:
func : 自定义函数
iterable : 可迭代型数据(容器类型数据,range对象,迭代器)
返回值:
迭代器
lst = [1,2,3,4,5,6,7,8,9,10]
# filter + lambda 改写
it = filter(lambda i : True if i % 2 == 0 else False , lst )
print(list(it))
3.reduce
reduce(func,iterable)
功能:计算数据
先把iterable中的前两个值拿出来,扔到func当中做运算,
把计算的结果和iterable中的第三个元素在扔到func当中做运算,
再把结果算出来,和第四个元素做运算,以此类推
直到所有结果运算完毕.返回该结果
参数:
func : 自定义函数
iterable : 可迭代型数据(容器类型数据,range对象,迭代器)
返回值:
计算之后的结果
从 … 导入 …
from functools import reduce
题目1
lst = [5,4,8,8] # => 整型5488
# 常规写法
# 方法一
strvar = ""
for i in lst:
strvar += str(i)
print(strvar , type(strvar))
res = int(strvar)
print(res , type(res))
# 方法二
"""
5*10 + 4 = 54
54*10 + 8 = 548
548 * 10 + 8 = 5488
"""
from collections import Iterator,Iterable
lst = [5,4,8,8]
it = iter(lst)
print(isinstance(it , Iterator))
print(isinstance(it , Iterable))
num1 = next(it)
num2 = next(it)
print(num1,num2)
num = num1 * 10 + num2
print(num) # 54
for i in it:
num = num * 10 + i # 54 * 10 + 8 => 548
print(num, type(num))
# reduce 改造
def func(x,y):
return x*10 + y
lst = [5,4,8,8]
res = reduce(func,lst)
print(res , type(res))
# 使用reduce + lambda改造
res = reduce(lambda x,y:x*10+y,lst)
print(res)
题目2
“789” => 789 禁止使用int强转
# "789" -> 数字7 数字8 数字9
def func1(x,y):
return x*10 + y
def func2(n):
dic = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9}
return dic[n]
it = map(func2,"789") # [7,8,9]
res = reduce(func1,it)
print(res,type(res))
4.sorted
sorted(iterable,key=函数,reverse=False)
功能:排序
参数:
iterable:可迭代型数据(容器类型数据,range对象,迭代器)
key :指定自定义函数或内置函数
reverse :代表升序或者降序 , 默认是升序(从小到大排序) reverse=False
返回值:
排序后的结果
1.默认是从小到大排序
lst = [1,2,3,4,5,-90,-4,-1,100]
res = sorted(lst)
print(res)
2.reverse 从大到小排序
res = sorted(lst,reverse=True)
print(res)
3.指定函数进行排序
# 按照绝对值排序 abs
lst = [-10,-1,3,5]
res = sorted(lst,key=abs)
print(res)
4.使用自定义函数进行排序
lst = [19,21,38,43,55]
def func(n):
return n % 10
lst = sorted(lst,key=func)
print(lst)
sorted 和 sort 之间的区别
# 字符串
container = "eadc"
# 列表
container = [19,21,38,43,55]
# 元组
container = (19,21,38,43,55)
# 集合
container = {19,21,38,43,55}
# 字典 (排序的是字典的键)
container = {"c":3,"a":1,"b":2}
container = {"王闻":3,"高云峰":2}
print("<===>")
res = sorted(container)
print(res)
# (1) sorted可以排序一切容器类型数据, sort只能排列表
# (2) sorted返回的是新列表,sort是基于原有的列表进行修改
# (3) 推荐使用sorted