python教程20-filiter,map,reduce方法、内置函数、高阶函数

    科技2022-07-20  163

    python教程_小白入门2020/10/5

    学习目标

    文章目录

    python教程_小白入门2020/10/5P125 filiter&map&reduce方法P126 内置函数总结P127 高阶函数1P128 高阶函数2

    P125 filiter&map&reduce方法

    from functools import reduce # 导入模块的语法 # filter-过滤(使用的最多) map-执行一定的操作 reduce # author by Zhang_jiongjiong # filter过滤的意思 # python2里是内置函数,python3里是内置类 # 对可迭代对象进行过滤,得到一个filter对象 # filter 可以给定两个参数,第一个参数是函数,第二个参数是可迭代对象 ages = [12, 23, 30, 17, 16, 22, 19] # ele所代表的是一个元素,元素的指标是要大于18 x = filter(lambda ele: ele > 18, ages) print(x) # <filter object at 0x000001F579E1BBC8>,filter类型的对象 # filter对象也是 一个可迭代对象 # for a in x: # print(a) # 23 # 30 # 22 # 19 adult = list(x) print(adult) # [23, 30, 22, 19] # --------------------------------------------------------- # map的使用 ages1 = [12, 23, 30, 17, 16, 22, 19] m = map(lambda ele: ele + 2, ages1) print(list(m)) # [14, 25, 32, 19, 18, 24, 21] # ------------------------------------------------------------- # reduce 的使用 # reduce以前是一个内置函数, # 内置函数和内置类都在 builtin.py文件里,后来给放到了一个模块中 scores = [100, 89, 76, 87] scores1 = [100, 89, 76, 87] ss = reduce(lambda ele1, ele2: ele1 + ele2, scores) # 作用类似于 (((1+2)+3)+4)+5 print(ss) # 352 def foo(x, y): return x + y print(reduce(foo, scores1)) # 352,注意这里的foo是没有括号的 # ------------------------------------------- # reduce 练习 # 统计所有student的年龄加和 students = [ {'name': 'zhangsan', 'age': 22, 'score': 98, 'height': 180}, {'name': 'lisi', 'age': 21, 'score': 97, 'height': 185}, {'name': 'jack', 'age': 22, 'score': 100, 'height': 175}, {'name': 'tony', 'age': 23, 'score': 90, 'height': 176}, {'name': 'herry', 'age': 20, 'score': 95, 'height': 172} ] # def bar(x, y): # # x = {'name': 'zhangsan', 'age': 22, 'score': 98, 'height': 180},y = {'name': 'lisi', 'age': 21, 'score': 97, 'height': 185}, # # x = 39,y={'name': 'jack', 'age': 22, 'score': 100, 'height': 175}, # return x['age']+y['age'] #会报错 # reduce(bar, students) # ------------------------------------------------------------------- # def bar(xx, yy): # # x = 0 # # y ={'name': 'zhangsan', 'age': 22, 'score': 98, 'height': 180}, # return xx + yy['age'] # print(reduce(bar, students, 0)) # x初始化的值是0 #108 # ---------------------------------------------------------------------- print(reduce(lambda xxx, yyy: xxx + yyy['age'], students, 0)) # 108

    P126 内置函数总结

    # 内置函数的总结 # author by Zhang_jiongjiong # 内置函数分类1.数学类--如abs()求绝对值 # 2.可迭代对象相关方法--如all(),any(),iter:获取到可迭代对象的迭代器,next():for in # 3.转换相关的bin()--将数字转换为二进制,chr()-将对应的字符编码转换为字符char(97)==>a # ord()将字符转换为对应的编码 # 4.dir()列出对象可以使用的所有方法 # 5.divmod()-求两个数的商和余数 # 6.eval:执行字符串里的python代码 # 7.变量相关的--globls(),local() # 8.help()--查看帮助文档 # 9.id()获取一个数据的内存地址 # 10.输入输出相关 print() input() # 11.判断对象相关,isinstance判断一个对象是否是由一个类创建出来 # issubclass:判断一个类是不是另一个类的子类 # 12. # # #all()布尔类型判断,将所有的元素都转为布尔值,然后进行判断 print(all(['hello', 'good', 'yes'])) # True print(all(['hello', 0])) # False print(any(['hello', 'good', 'yes'])) # True print(any(['hello', 0])) # True # dir会列出nums支持的所有方法 nums = [1,2,3] print(dir(nums)) # ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] print(dir('hello')) #['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] shang,yushu = divmod(15,2) print(shang,yushu) # 7 1

    P127 高阶函数1

    # 高阶函数的基本使用 # author by zhangjiong # 高阶函数的使用场景: # 1.一个函数作为另一个函数的返回值 # 2.一个函数作为另一个函数的参数 # 3.函数内部再定义一个函数 # 函数和函数之间调用 #---------------------- # 加括号和不加括号调用的区别 # 加括号:函数调函数,最后返回的是一个具体的值 def foo(): print("我是foo,我被调用了") return 'foo' def doo(): print('我是doo,我被调用了') return foo() doo() # 我是doo,我被调用了 # 我是foo,我被调用了 x = doo() print(x) # foo 这里的x是一个值 #--------------------------- # 不加括号:函数调函数,返回的是一个函数 def aoo(): print("我是aoo,我被调用了") return 'aoo' def boo(): print('我是boo,我被调用了') return aoo y = boo() print(y) # 我是boo,我被调用了 这里的y是函数 # <function aoo at 0x0000029ADC4F5F78> y() # 我是aoo,我被调用了 # 这就相当于 是 新定义了一个函数y(),y()函数的功能是和aoo()一摸一样的 boo()() # boo()就等于aoo,boo()()就等于 aoo() # 我是boo,我被调用了 # 我是aoo,我被调用了 z = boo()() print(z) #aoo # 一定要弄清楚返回的是一个具体的值还是 返回的是一个函数 # ---------------------------------------- # 装饰器 # 函数里边套函数 def outer(): m=100 # inner函数只能在outer函数内部使用 def inner(): n = 90 print("我是inner函数") print("我是outer函数") return inner #outer() #我是outer函数 outer()() # 我是outer函数 # 我是inner函数

    P128 高阶函数2

    #  高阶函数2 # author by zhangjiong # 高阶函数的使用场景: # 1.一个函数作为另一个函数的返回值 def test(): print("我是test函数") return 'hello' def demo(): print("我是demo函数") return test def bar(): print("我是bar函数") return test() x = test() print(x) # 我是test函数 # hello y = demo() # 我是demo函数,这里的y相当于test函数 y() # 我是demo函数 # 我是test函数 z = bar() # 这里的z相当于 一个变量 print(z) # 我是bar函数 # 我是test函数 # hello # 2.一个函数作为另一个函数的参数 # sort filter map # 3.函数内部再定义一个函数

    有括号就是代表调用了函数,没括号就是代表返回的值是这个函数,返回的是函数类型。

    Processed: 0.011, SQL: 8