day13:迭代器&高阶函数(map,reduce,filter,sorted)
迭代器 1.迭代器的定义: 能被next调用,并不断返回下一个值的对象,叫做迭代器(对象) 2.迭代器的概念: 迭代器指的是迭代取值的工具,迭代是一个重复的过程, 3.迭代器的特征:?并不依赖索引,而通过next指针迭代所有数据,一次只取一个值,大大节省空间 4.dir:?获取当前类型对象中的所有成员 其中,里面有一个魔术方法,叫做__iter__方法, __iter__方法用来判断是否是可迭代性数据 setvar = {"a",bcd"} lst = dir(setvar) # 获取setvar对象中的所有成员 print(lst) ''' ['__and__','__class__','__contains__','__delattr__','__dir__','__doc__','__eq__','__format__','__ge__','__getattribute__','__gt__','__hash__','__iand__','__init__','__init_subclass__','__ior__','__isub__','__iter__','__ixor__','__le__','__len__','__lt__','__ne__','__new__','__or__','__rand__','__reduce__','__reduce_ex__','__repr__','__ror__','__rsub__','__rxor__','__setattr__','__sizeof__','__str__','__sub__','__subclasshook__','__xor__','add','clear','copy','difference','difference_update','discard','intersection','intersection_update','isdisjoint','issubset','issuperset','pop','remove','symmetric_difference','symmetric_difference_update','union','update'] ''' 5.关于迭代器,需要注意的点: for 循环能够遍历一切可迭代性数据的原因在于,1)">底层调用了迭代器,通过next方法中的指针实现数据的获取 可迭代对象(不能够被next直接调用)? -> 迭代器(可以被next直接调用的过程)? 一个可迭代对象不一定是迭代器 一个迭代器就一定是一个可迭代对象 ? 6.定义一个迭代器 定义迭代器需要使用iter()方法 setvar = {} it = iter(setvar) 将可迭代对象setvar变成了迭代器 print(it) <set_iterator object at 0x000002142D1108B8> 7.判断一个迭代器 迭代器必须要有__iter__方法和__next__方法 res = __iter__" in dir(it) and __next__in dir(it) print(res) True 8.调用迭代器 next在调用迭代器中的数据时,是单向不可逆,一条路走到黑的过程 当没有可迭代的数据后,抛出一个StopIteration的异常,并且停止迭代 setvar = { 将可迭代对象setvar变成了迭代器 res = next(it) c res = d res = a res = b res = 当没有可迭代的数据后,抛出一个StopIteration的异常,并且停止迭代 9.重置迭代器 重置迭代器,只需要再次调用iter()方法即可 it = iter(setvar) 重置迭代器 res =print(res) 10.使用其他方式判断是否是可迭代对象/迭代器 """Iterator 迭代器 Iterable 可迭代对象""" from collections import Iterator,Iterable it = iter(setvar) res = isinstance(it,Iterator) (res) res =print(res) 11.除了next(),也可以使用以下两种方式调用迭代器中的数据 1. for 循环 print(<=>) for i it: (i) 2. for + next ) lst = [1,2,3,4,5,6,7,8,9,10] it = iter(lst) in range(10): res = next(it) (res) (next(it)) print(next(it)) 高阶函数 高阶函数的定义:能够把函数当成参数传递的就是高阶函数 常用的四大高阶函数:map filter reduce sorted 1.map map(func,Iterable) 示例1:将["1","2","3","4"]转化成[1,4] 常规写法: lst = [1234] 常规写法 lst_new = [] lst: lst_new.append(int(i)) print(lst_new) 用map改造的写法: map改造 it = map(int,lst) it是一个map对象 print(list(it)) [1,4] map(int,lst)的实现过程: 首先把"1" 扔到int当中做处理,将强转后的结果扔到迭代器中 获取迭代器中的数据的方法:1.next()? 2.for循环遍历? 3.for+next? 4.list(it)强转 ? 示例2:[1,4] => [2,24,64] map改造 参数和返回值return一定要写''' def func(n): return n << n lst = [1,4] it = map(func,lst) (list(it)) lambda + map it = map(lambda n : n << n,1)">print(list(it)) 示例3:dic = {97:"a",98:"b",99:"c"} # ["a","b","c"] => ascii [97,98,99] map改造 原字典 dic = {97:} 新字典 dic_new = {} 遍历原字典 for k,v dic.items(): 更换键值对 dic_new[v] = k print(dic_new) {'a': 97,'b': 98,'c': 99} 通过键来获取值 return dic_new[n] lst = [print(list(it)) 2.filter filter(func,iterable) lst = [1,1)">] lst: if i % 2 == 0: lst_new.append(i) (lst_new) filter改写 func(i): 0: True else: False it = filter(func,lst) filter + lambda 改写 it = filter(lambda i : True if i % 2 == 0 False,lst ) print(list(it)) 3.reduce reduce(func,iterable) 功能:计算数据 示例1:lst = [5,8]? => 整型5488 常规写法1: strvar = "" lst: strvar += str(i) (strvar,type(strvar)) res = int(strvar) print(res,type(res)) 常规写法2: print(isinstance(it,Iterator)) True num1 = next(it) 5 num2 = next(it) 4 num = num1 * 10 + num2 54 it: num = num * 10 + i 54*10+8=548 548*10+8=5488 print(num,type(num)) 5488 <class 'int'> reduce改造: func(x,y): return x*10 + y lst = [5,1)">] res = reduce(func,type(res)) reduce(func,lst)实现的过程: 先拿出5和4两个元素,扔到func当中做运算,结果是54 ? reduce+lambda: res = reduce(lambda x,y:x*10+y,1)">print(res) 示例2:"789"=>789 禁止使用int强制转换 func1(x,1)"> y func2(n): dic = {0":0,1)">":1,1)">":2,1)">":3,1)">":4,1)">5":5,1)">6":6,1)">7":7,1)">8":8,1)">9":9 dic[n] it = map(func2,1)">789") [7,9] res = reduce(func1,it) 4.sorted |
- django – 我需要一个ModelMultipleChoiceField的“原始id”
- python – 使用对流层进行云形成,如何在标记中添加“在发布
- python – 在hybrid_property中Flask-SQLAlchemy多对多有序
- 如何使用Python(scikit-learn)计算FactorAnalysis得分?
- Python使用tablib生成excel文件的简单实现方法
- python+scrapy分析博客园首页4000篇优质博客(图解)
- 小白学Python(18)——pyecharts 关系图 Graph
- python – 来自提及的Tweepy用户ID
- python数据库操作常用功能使用详解(创建表/插入数据/获取数
- Python实现单词翻译功能