编程的方法论

面向过程:找到问题的

函数式:不可变、不用变量保存状态、不修改变量

面向对象:

高阶函数:

满足俩个特性任意一个即为高阶函数

1.函数的传入参数是一个函数名

2.函数的返回值是一个函数名

append() 方法用于在列表末尾添加新的对象。

map函数:

num_l=[1,2,10,5,3,7]# 计算该列表中数的平方值
方法一:
# ret=[]
# for i in num_l:
#     ret.append(i**2)
# print(ret)
方法二:
def map_test(array):ret=[]for i in num_l:ret.append(i**2)   #
return ret
ret=map_test(num_l)
print(ret)方法三:
num_l=[1,2,10,5,3,7]#lambda x:x+1def add_one(x):    return x+1#lambda x:x-1def reduce_one(x):    return x-1#lambda x:x**2def square_one(x):    return x**2def map_test(func,array):    ret=[]    for i in num_l:        res=func(i)        ret.append(res)    return retprint(map_test(add_one,num_l))print(map_test(reduce_one,num_l))#print(map_test(lambda x:x**2,num_l))print(map_test(square_one,num_l))
#终极版本num_l=[1,2,10,5,3,7]def map_test(func,array):    ret=[]    for i in num_l:        res=func(i) #add_one        ret.append(res)    return ret

print(map_test(lambda x:x+1,num_l))res=map(lambda x:x+1,num_l)print('内置函数map,处理结果',res)# for i in res:#     print(i)print(list(res))print('传的是有名函数',list(map(reduce_one,num_l)))


#大写转换msg='wuxiping'print(list(map(lambda x:x.upper(),msg)))

 filter函数:

# movie_people=['sb_alex','sb_wupeiqi','linhaifeng','sb_yuanhao']
# def filter_test(array):
#     ret=[]
#     for p in array:
#         if not p.startswith('sb'):
#             ret.append(p)
#     return ret
# print(filter_test(movie_people))# movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
# def sb_show(n):
#     return n.endswith('sb')
# def filter_test(func,array):
#     ret=[]
#     for p in array:
#         if not func(p):
#             ret.append(p)
#     return ret
# res=filter_test(sb_show,movie_people)
# print(res)
#终极版本
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
# def sb_show(n):
#     return n.endswith('sb')
#lambda n:n.endswith('sb')
def filter_test(func,array):ret=[]for p in array:if not func(p):ret.append(p)return ret
res=filter_test(lambda n:n.endswith('sb'),movie_people)
print(res)
#filter函数movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']#print(filter(lambda n:n not n.endswith('sb',movie_people)))res=filter(lambda n:not n.endswith('sb'),movie_people)print(list(res))

 reduce函数:

from functools import reduce
# num_l=[1,2,3,100]
# res=0
# for num in num_l:
#     res+=num
# print(res)#相加
# num_l=[1,2,3,100]
# def reduce_test(array):
#     res=0
#     for num in num_l:
#         res+=num
#     return res
# print(reduce_test(num_l))#相乘
# num_l=[1,2,3,100]
# def reduce_test(func,array):
#     res=array.pop(0)
#     for num in array:
#         res=func(res,num)
#     return res
# print(reduce_test(lambda x,y:x*y,num_l))# 指定初始值
# num_l=[1,2,3,100]
# def reduce_test(func,array,init=None):
#     if init is None:
#         res=array.pop(0)
#     else:
#         res=init
#     for num in array:
#         res=func(res,num)
#     return res
# print(reduce_test(lambda x,y:x*y,num_l,100))#reduce函数
num_l=[1,2,3,100]
print(reduce(lambda x,y:x+y,num_l,1))
print(reduce(lambda x,y:x+y,num_l,))

总结:

#总结:
# map函数:处理序列中的每个元素,得到的结果是一个‘列表’,该‘列表’元素个数和位置与原来一样
#filter函数:遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下来得到的结果是一个‘列表’
# people=[{'name':'alex','age':10000},
#         {'name': 'wupeiqi', 'age': 10000},
#         {'name': 'yuanhao', 'age': 9000},
#         {'name': 'linhaifeng', 'age': 18},]
# print(list(filter(lambda p:p['age']<=18,people)))
#reduce函数:处理 一个序列,把序列进行合并操作
# num_l=[1,2,3,100]
# print(reduce(lambda x,y:x+y,num_l,1))
# print(reduce(lambda x,y:x+y,num_l,))

内置函数:

# print(abs(-1))
# print(all([1,2,'1']))#做布尔运算(所有的都是True才返回True)
# name='你好'
# print(bytes(name,encoding='utf-8'))   #编码,三个字节
# print(bytes(name,encoding='utf-8').decode('utf-8'))# 解码
# print(bytes(name,encoding='gbk'))#两个字节
# print(bytes(name,encoding='gbk').decode('gbk'))
# #print(bytes(name,encoding='ascii'))#ascii 不能编码中文
# print(chr(100))
# print(dir(all))
# print(divmod(10,3))
# dic={'name':'alex'}
# dic_str=str(dic)
# print(dic_str)
# print(eval(dic_str))eval()函数:以Python的方式解析并执行字符串,并将返回的结果输出
# d1=eval(dic_str)#把字符串中的数据结构给提取出来
# print(d1['name'])
# a='1+2*(3/3-1)-2'#把字符串中的表达式进行运算
# print(eval(a))
#可hash的数据类型即不可变数据类型,不可hash的数据类型即可变数据类型
# print(hash('dhfsaklefownfs2134254'))
# print(hash('-03thjsdnfkgqopw3utjlsdfml;'))
# name='alex'
# print(hash(name))
# print(hash(name))
# print(hash(name))
# print(help(dir))
# print(bin(10))  #10进制转换成2进制
# print(hex(12))   #16
# print(oct(12))   #8
# print(isinstance(1,int))
# print(isinstance('abc',int))
# print(isinstance('abc',str))
name='哈哈哈哈'
print(globals())
print(__file__)
print(locals())

day16——函数式编程和内置函数相关推荐

  1. Python基础11-函数式编程与内置函数

    目录 函数即变量 lambda关键字定义匿名函数 高阶函数 内置函数map 内置函数filter 内置函数reduce 内置函数看文档 函数即变量 书接上回,Python里面,函数就是变量,可以被当成 ...

  2. python编程内置函数使用方法_python编程(4)--内置函数

    ​     函数,通常称为方法,是一种将自变量到因变量的映射(y = f(x)).在python里用def或者lambda去构造,语法如下. def f(x):      #x -- 输入 y = x ...

  3. python入门之函数调用内置函数_第九篇 python基础之函数,递归,内置函数

    阅读目录 一 数学定义的函数与python中的函数 二 为何使用函数 背景提要 三 函数和过程 四 函数参数 五 局部变量和全局变量 六 前向引用之'函数即变量' 七 嵌套函数和作用域 八 递归调用 ...

  4. 第七篇 python基础之函数,递归,内置函数

    阅读目录 一 数学定义的函数与python中的函数 二 为何使用函数 背景提要 三 函数和过程 四 函数参数 五 局部变量和全局变量 六 前向引用之'函数即变量' 七 嵌套函数和作用域 八 递归调用 ...

  5. python内置高阶函数求导_Python——函数式编程、高阶函数和内置函数,及

    Python--函数式编程.高阶函数及内置函数 函数式编程 一.不可变数据:不用变量保存状态不修改变量 二.第一类对象:函数即"变量" 1.函数名可以当做参数传递 2.返回值可以是 ...

  6. python3_函数_形参调用方式 / 不定长参数 / 函数返回值 / 变量作用域 / 匿名函数 / 递归调用 / 函数式编程 / 高阶函数 / gobal和nonlocal关键字 / 内置函数

    1.形参的调用方式 1. 位置参数调用 2. 关键词参数调用 原则: 关键词参数调用不能写在位置参数调用的前边 def test1(name, age):print("name:" ...

  7. 递归函数与内置函数和函数式编程

    递归函数: 定义:在函数内部,可以调用其他函数.如果一个函数在内部调用自身, 这个函数就是递归函数. 实例1(阶乘): 在这里插入代码片def factorial(n):result=nfor i i ...

  8. 在python中用递归的方法编程_python基础之函数,递归,内置函数

    阅读目录 一 数学定义的函数与python中的函数 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量, ...

  9. Day07:常用模块,面向对象编程(对象类)及内置函数

    今日内容: 1.常用模块 2.面向对象编程(*****)     介绍面向对象编程     类     对象 3.内置函数 ------------------------------ 1.面向过程编 ...

最新文章

  1. 南京晓庄学院大一第二学期计算机数据结构期末考试试卷及答案,南京晓庄学院数据结构题库参考答案.docx...
  2. 怎样使一个Android应用不被杀死?(整理)
  3. 如何创建高质量的TypeScript声明文件(六) - 示例
  4. 【Python】集合类型产生KeyError异常原因及数据去重
  5. 关于hexo与github使用过程中的问题与笔记
  6. python设置本机IP地址、子网掩码、DNS,获取本机IP地址、子网掩码、DNS、MAC
  7. serversocket中的backlog是什么_输入网址按回车,到底发生了什么
  8. JZ The First Day 总结
  9. 微信小程序中页面间跳转传参方式
  10. Swift4-有妖气漫画精仿框架部分
  11. 谈谈核心网UPF和开放
  12. ServiceLoader详解
  13. 三亿顶级流量社区,下厨房APP变现三部曲
  14. mac笔记本怎么外接显示屏_苹果MAC笔记本怎么外接显示器
  15. numpy之生成随机数
  16. 【Python】list写入txt文件
  17. Echarts图表插件(4.x版本)使用(二、带分类筛选的多个图表/实例化多个ECharts,以关系图/force为例)...
  18. android 三种常用的加密方式
  19. 是时候让企业拥有“AI自由”了
  20. 第一次上计算机课心得,第一次上微机课作文4篇

热门文章

  1. su组件在什么窗口_Su与Rhino互导注意事项
  2. postman请求soap 请求_postman中请求如何传递对象到spring controller?
  3. centos mysql 5.6.36_CentOS 6.9 升级MySQL 5.6.36到5.7.18
  4. STM32分类及命名方法
  5. 通过文件IO控制硬件设备的方法
  6. 请求模式解决共享资源冲突
  7. C++版数据结构继承关系图
  8. 按钮button的常用属性和事件
  9. python3.6 3.7共存_[转]CentOS 7安装Python3.6过程(让linux系统共存Python2和Python3环境)...
  10. 加拿大留学商科好还是计算机科学好,去加拿大读商科专业就是要选择这些才最好!...