编程的方法论

面向过程:找到问题的

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

面向对象:

高阶函数:

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

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——函数式编程和内置函数的更多相关文章

  1. Python全栈开发之3、深浅拷贝、变量和函数、递归、函数式编程、内置函数

    一.深浅拷贝 1.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy # 定义变量 数字.字符串 # n1 = 123 n1 ...

  2. Python函数式编程:内置函数reduce 使用说明

    一.概述 reduce操作是函数式编程中的重要技术之一,其作用是通过对一个集合的操作,可以从中生成一个值.比如最常见的求和,求最大值.最小值等都是reduce操作的典型例子.python通过内置red ...

  3. Python函数式编程:内置函数map()使用说明

    一.概述 map操作是函数式编程中的重要技术之一,其作用就是对一个集合中的每个元素做处理,生成一个新的元素,由这些新的元素组成一个新的集合的返回. 所以map操作后,产生的新集合的元素个数和原集合的元 ...

  4. day22 yield的表达式的应用,面向过程编程,内置函数前几个

    Python之路,Day10 = Python基础10 生成器表达式: (i for i in range(10) if i > 5)os.walk(r'文件路径')返回一个迭代器, 第一次ne ...

  5. Python函数式编程:内置filter函数使用说明

    filter操作是函数式编程中对集合的重要操作之一,其作用是从原集合中筛选符合条件的条目,组成一个新的集合. 这在我们日常编程中是非常常见的操作.我们通常的做法是通过循环语句来处理. 而使用filte ...

  6. day16_函数作用域_匿名函数_函数式编程_map_reduce_filter_(部分)内置函数

    20180729    补充部分代码 20180727    上传代码 #!/usr/bin/env python # -*- coding:utf-8 -*- # ***************** ...

  7. python学习-day16:函数作用域、匿名函数、函数式编程、map、filter、reduce函数、内置函数r

    一.作用域 作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变 二.匿名函数 lambda:正常和其他函数进行配合使用.正常无需把匿名函数赋值给一个变量. f=lambda x:x*x p ...

  8. 简学Python第三章__函数式编程、递归、内置函数

    #cnblogs_post_body h2 { background: linear-gradient(to bottom, #18c0ff 0%,#0c7eff 100%); color: #fff ...

  9. python_08 函数式编程、高阶函数、map、filter、reduce函数、内置函数

    函数式编程 编程方法论: 1.面向过程 找到解决问题的入口,按照一个固定的流程去模拟解决问题的流程 (1).搜索目标,用户输入(配偶要求),按照要求到数据结构内检索合适的任务 (2)表白,表白成功进入 ...

随机推荐

  1. Laravel-任务调度

    简介Cron 是 UNIX.SOLARIS.LINUX 下的一个十分有用的工具,通过 Cron 脚本能使计划任务定期地在系统后台自动运行.这种计划任务在 UNIX.SOLARIS.LINUX下术语为 ...

  2. “纽劢科技杯”第十六届同济大学程序设计竞赛暨上海邀请赛同步赛 J-张老师的游戏

    传送门 题目描述     在空闲时间,张老师习惯性地和菜哭武玩起了取石子游戏,这次的游戏规则有些不同,在他们面前有n堆石子,其中,第i堆石子的个数为a[i],现在制定规则如下:     从张老师开始, ...

  3. js 替换所有指定的字符串

    js 的replace方法只替换第一个匹配到的的字符 如果要全局替换,使用以下方法(g为全局标志) str.replace(/需要替换的字符串/g,"新字符串") //如果有特殊符 ...

  4. 数据分析三剑客之numpy

    Numpy 简介 数据分析三剑客:Numpy,Pandas,Matplotlib NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算, ...

  5. 一道php笔试题

    原文地址: http://www.walu.cc/php/a-bishiti.md 问题: 请找出下面代码中的问题,修复并优化. <?php //批量注册用户,每次>100个. //注册新 ...

  6. [洛谷P1842] 奶牛玩杂技

    题目类型:贪心+证明,经典题 传送门:>Here< 题意:有\(N\)头奶牛,每个奶牛有一个重量\(W[i]\),力量\(S[i]\).定义每个奶牛的压扁程度为排在它前面的所有奶牛的总量之 ...

  7. 【LOJ6482】LJJ 爱数数 数论

    题目大意 给你 \(n\),求 \[ \sum_{a=1}^n\sum_{b=1}^n\sum_{c=1}^n[\frac{1}{a}+\frac{1}{b}=\frac{1}{c}][\gcd(a, ...

  8. Linux-存储管理

    存储基础知识 从工作原理区分: 机械   HDD 固态   SSD SSD的优势: SSD是摒弃传统磁介质,采用电子存储介质进行数据存储和读取的一种技术,突破了传统机械硬盘的性能瓶颈,拥有极高的存储性 ...

  9. 命令级的python静态资源服务。

    python -m SimpleHTTPServer 在当前目录起python静态资源服务.

  10. Vue(小案例_vue+axios仿手机app)_购物车(计算商品总金额)

    一.前言                 1.计算总金额                 2.点击删除按钮,删除对应的商品信息                 3.当还没结算的时候,当用户跳到其他页面 ...