# ### 迭代器:能被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.重置迭代器
it = iter(setvar) res = next(it) print(res)
5.使用其他方式判断迭代器或者可迭代对象
"""Iterator 迭代器 Iterable 可迭代对象""" # from ... 从哪里 import 引入 ... from collections 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(10):
res = next(it)
print(res)
print(next(it))
print(next(it))
# ### 高阶函数 : 能够把函数当成参数传递的就是高阶函数
"""
map(func,Iterable)
功能:处理数据
把Iterable中的数据一个一个拿出来,扔到func函数中做处理
把处理之后的结果放到迭代器当中,最后返回迭代器
参数:
func : 自定义函数 或 内置函数
Iterable : 可迭代性数据(容器类型数据 range对象 迭代器)
返回值:
迭代器
"""
(1) lst = ["1","2","3","4"] # [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))
# ### 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)) """ # 代码解析: 先拿出5和4两个元素,扔到func当中做运算,结果是54 在拿54和8两个元素,扔到func当中做运算,结果548 在拿548和8两个元素,扔到func当中做运算,结果5488 返回最终的结果: 5488 程序结束 """
使用reduce + lambda改造
res = reduce(lambda x,y:x*10+y,lst) print(res)
题目2
"789" => 789 禁止使用int强转
def func(x,y):
return x*10 + y
strvar = "789"
res = reduce(func,list(strvar))
print(res , type(res))
"""
77777777778 * 10 + 9
777777777787777777777877777777778777777777787777777777877777777778777777777787777777777877777777778777777777789 <class 'str'>
"""
# "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))
# ### 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) """ -1 => abs(-1) => 1 3 => abs(3) => 3 5 => abs(5) => 5 -10 => abs(-10) => 10 [-1, 3, 5, -10] """ print(res)
4.使用自定义函数进行排序
lst = [19,21,38,43,55]
def func(n):
return n % 10
lst = sorted(lst,key=func)
print(lst)
"""
21 => n % 10 => 1
43 => n % 10 => 3
55 => n % 10 => 5
38 => n % 10 => 8
19 => n % 10 => 9
21 43 55 38 19
"""
# ### 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