编程的方法论

面向过程:找到问题的

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

面向对象:

高阶函数:

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

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. VS编程,C#串口通讯,通过串口读取数据的一种方法

    一.可能需要的软件:1.虚拟串口vspd(Virtual Serial Port Driver,用来在电脑上虚拟出一对串口,模拟通讯. 2.友善串口调试助手,用来发送.读取数据. 二.思路1.查询本机 ...

  2. Idea中最最常见的快捷键

    掌握如下快捷键,基本就够用了.没必要记那么多. Ø  命令:Ctrl+Shift+A可以查找所有Intellij的命令,并且每个命令后面还有其快捷键.所以它不仅是一大神键,也是查找学习快捷键的工具. ...

  3. pytorch识别CIFAR10:训练ResNet-34(准确率80%)

    版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com CNN的层数越多,能够提取到的特征越丰富,但是简单地增加卷积层数,训练时会导致梯度弥散或梯度爆炸. 何 ...

  4. RabbitMQ的一些有用教程

    最近学习了一些RabbitMQ的知识,现在对所阅读过的一些非常优秀的教程进行总结,感谢各位博主和大神的无私奉献. 一.原理篇 https://blog.51cto.com/lookingdream/2 ...

  5. dict、defaultdict 和 OrderedDict 比较

    一.dict.defaultdict 和 OrderedDict 常见的方法比较 dict.defaultdict 和 OrderedDict 常见的方法比较   dict defaultdict O ...

  6. DAY19、日常模块

    一.hashlib模块:加密1.基本使用:import hashlibcipher = hashlib.md5('需要加密的数据(二进制形式)'.encode('utf-8'))print(ciphe ...

  7. Cordova Upload Images using File Transfer Plugin and .Net core WebAPI

    In this article, I am going to explain ,"How to Upload Images to the server using Cordova File ...

  8. [2019.04.16] 由Python写成的自动解压脚本

    很久很久以前(二十七天吧……大概)被要求写一个脚本来检索并解压磁盘上所有的以特定格式命名的tar文件,于是乎学习和摸鱼就一起开始了. 这次要写的脚本,针对的是这样的文件结构: 文件结构如上图所示 可以 ...

  9. BZOJ5507 GXOI/GZOI2019旧词 (树链剖分+线段树)

    https://www.cnblogs.com/Gloid/p/9412357.html差分一下是一样的问题.感觉几年没写过树剖了. #include<iostream> #include ...

  10. grafana备份

    #!/bin/bash #自动备份grafana数据库并上传到云盘 NOWDATE=`date +%Y-%m-%d` YUNPAN_USER=xxxx YUNPAN_PASSWD=XXXXXXXXXX ...