编程的方法论

面向过程:找到问题的

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

面向对象:

高阶函数:

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

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. Unity TimeLine

    最近一直再看这方面的内容,看的比较多知识点比较分散,所以目的就是把这些知识点内容梳理一边,并作记录. PlayableDirector与TrackAsset,TrackAsset与PlayableAs ...

  2. Node.js中的console.log()输出彩色字体

    转自:https://www.jianshu.com/p/cca3e72c3ba7 console.log('\033[42;30m DONE \033[40;32m Compiled success ...

  3. OpenCV4.1.0实践(3) - 图片缩放

    简单的案例: (1)通过比例进行缩放 import cv2 as cv import numpy as np # 图片缩放 img = cv.imread('images/animal.jpg', f ...

  4. IntelliJ IDEA远程调试(Debug)Tomcat

    为什么需要这么做? 解决 在我本地是好的啊 这个世界性难题- 测试环境碰到问题,直接连上debug,不用再测试本地,再查看测试环境日志 遇到一些诡异的问题,日志是看不出端倪的 调试一些只能在测试环境执 ...

  5. [BJOI2019]排兵布阵——分组背包

    题目链接: [BJOI2019]排兵布阵 对于每座堡垒,将$s$个对手排序,显然如果安排的兵力能打败第$i$个对手就一定能打败前$i-1$个. 那么对于第$i$座城堡,可以看做有$s+1$个物品(可以 ...

  6. HF-01

    胡凡 本书在第2章对C语言的语法进行了详细的入门讲解,并在其中融入了部分C+的特性. 第3-5章是 入门部分. 第3章 初步训练读者最基本的编写代码能力: 第4章对 常用介绍,内容重要: 第5章是   ...

  7. BZOJ 3000: Big Number (数学)

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=3000 题解: 首先n很大,O(n)跑不过,那么就要用一些高端 而且没听过 的东西——sti ...

  8. go实现dgraph的各种操作

    go实现dgraph的各种操作 import "github.com/dgraph-io/dgo" import "github.com/dgraph-io/dgo/pr ...

  9. MySQL 水平拆分与垂直拆分详解

    前言:说到优化mysql,总会有这么个回答:水平拆分,垂直拆分,那么我们就来说说什么是水平拆分,垂直拆分. 一.垂直拆分 说明:一个数据库由很多表的构成,每个表对应着不同的业务,垂直切分是指按照业务将 ...

  10. python全栈开发中级班全程笔记(第二模块、第三章)(员工信息增删改查作业讲解)

    python全栈开发中级班全程笔记 第三章:员工信息增删改查作业代码 作业要求: 员工增删改查表用代码实现一个简单的员工信息增删改查表需求: 1.支持模糊查询,(1.find name ,age fo ...