day16——函数式编程和内置函数
编程的方法论
面向过程:找到问题的
函数式:不可变、不用变量保存状态、不修改变量
面向对象:
高阶函数:
满足俩个特性任意一个即为高阶函数
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+1
def add_one(x):
return x+1
#lambda x:x-1
def reduce_one(x):
return x-1
#lambda x:x**2
def square_one(x):
return x**2
def map_test(func,array):
ret=[]
for i in num_l:
res=func(i)
ret.append(res)
return ret
print(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——函数式编程和内置函数的更多相关文章
- Python全栈开发之3、深浅拷贝、变量和函数、递归、函数式编程、内置函数
一.深浅拷贝 1.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy # 定义变量 数字.字符串 # n1 = 123 n1 ...
- Python函数式编程:内置函数reduce 使用说明
一.概述 reduce操作是函数式编程中的重要技术之一,其作用是通过对一个集合的操作,可以从中生成一个值.比如最常见的求和,求最大值.最小值等都是reduce操作的典型例子.python通过内置red ...
- Python函数式编程:内置函数map()使用说明
一.概述 map操作是函数式编程中的重要技术之一,其作用就是对一个集合中的每个元素做处理,生成一个新的元素,由这些新的元素组成一个新的集合的返回. 所以map操作后,产生的新集合的元素个数和原集合的元 ...
- day22 yield的表达式的应用,面向过程编程,内置函数前几个
Python之路,Day10 = Python基础10 生成器表达式: (i for i in range(10) if i > 5)os.walk(r'文件路径')返回一个迭代器, 第一次ne ...
- Python函数式编程:内置filter函数使用说明
filter操作是函数式编程中对集合的重要操作之一,其作用是从原集合中筛选符合条件的条目,组成一个新的集合. 这在我们日常编程中是非常常见的操作.我们通常的做法是通过循环语句来处理. 而使用filte ...
- day16_函数作用域_匿名函数_函数式编程_map_reduce_filter_(部分)内置函数
20180729 补充部分代码 20180727 上传代码 #!/usr/bin/env python # -*- coding:utf-8 -*- # ***************** ...
- python学习-day16:函数作用域、匿名函数、函数式编程、map、filter、reduce函数、内置函数r
一.作用域 作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变 二.匿名函数 lambda:正常和其他函数进行配合使用.正常无需把匿名函数赋值给一个变量. f=lambda x:x*x p ...
- 简学Python第三章__函数式编程、递归、内置函数
#cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...
- python_08 函数式编程、高阶函数、map、filter、reduce函数、内置函数
函数式编程 编程方法论: 1.面向过程 找到解决问题的入口,按照一个固定的流程去模拟解决问题的流程 (1).搜索目标,用户输入(配偶要求),按照要求到数据结构内检索合适的任务 (2)表白,表白成功进入 ...
随机推荐
- To B Vs To C
谈谈 To B 业务的难点https://tangjie.me/blog/259.html
- Django后端向前端直接传html语言防止转义的方法(2种)
Django后端向前端直接传html语言防止转义的方法(2种) 目的,为了让前端对后端传输的这种方式不转义 1.使用mark_safe() from django.utils.safestring i ...
- 使用sqlserver 链接远程服务器进行查询
--创建链接服务器 exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' exec sp_addlinkedsrvlogi ...
- PHP 高级工程面试题汇总
PHP高级工程面试题汇总(2018.05) 1.给你四个坐标点,判断它们能不能组成一个矩形,如判断([0,0],[0,1],[1,1],[1,0])能组成一个矩形. 勾股定理,矩形是对角线相等的四边形 ...
- vue.js实战——props数据验证(自定义构造器检测)
Vue.component('my-component',{ props:{ //必须是数字类型 propA:Number, //必须是字符串或数字类型 propB:[String,Number], ...
- MyBatis-Plus
一.通用SQL 1.简介:(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 2.定义Javabean中成员变量所使用的的类型: ...
- pc端手機端自適應佈局方案
https://blog.csdn.net/chose_DoIt/article/details/80424341 https://blog.csdn.net/cxz792116/article/de ...
- react native使用百度echarts显示图表
echarts是百度推出的免费开源的图表组件,功能丰富,涵盖各行业图表.公司项目做h5项目用了不少,最近公司翻新h5页面,用react-native改造,来达到增强用户体验效果的目的.项目中遇到了一些 ...
- [HNOI/AHOI2018]毒瘤
题目描述 https://www.lydsy.com/JudgeOnline/upload/201804/%E6%B9%96%E5%8D%97%E4%B8%80%E8%AF%95%E8%AF%95%E ...
- Educational Codeforces Round 56 (Rated for Div. 2) D
给你一个无向图 以及点的个数和边 每个节点只能用1 2 3 三个数字 求相邻 两个节点和为奇数 能否构成以及有多少种构成方法 #include<bits/stdc++.h> usin ...