Python3 相关功能代码:
import io import sys import math # 调用reduce 函数 from functools import reduce sys.stdout = io.TextIOWrapper(sys.stdout.detach(),encoding='utf-8') # Python3 高阶函数之函数即变量 # print函数赋值给变量p p = print # 通过变量p 调用print函数 p("高阶函数之函数即变量") # Python3 高阶函数之传入函数 def add(x,y,z): return abs(x) + abs(y) result = add(-11,128,abs) print("result is:",result) # Python3 匿名函数 r = 10 area = lambda r:math.pi*r*r print('半价为',r,'的面积为:',area(r)) # Python3 map 函数 number =[1,2,3,4,5,6,7,8,9] print(list(map(lambda x: x*x, number))) # Python3 reduce 函数 number =[1,2,3,4,5,6,7,8,9] print(print(reduce(lambda x,y:x+y, number))) # Python3 filter 函数 st =['A', '', 'C', 'D', ''] print(list(filter(lambda x: x and x.strip(),st))) # Python3 sorted 函数之数字排序 a =[11,90,-1,20,17] print(sorted(a)) # Python3 sorted 函数之字符串排序 aa =['a','12','b','z'] print(sorted(aa))