---恢复内容开始---

一、filter函数

filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。

该接收两个参数,第一个为函数,第二个为序列,对序列中每个元素进行for循环,然后将每个元素传递给第一个位置的函数,然后返回 True 或 False,最后将返回 True 的元素放到新列表中

1、使用for循环将前面包含sb的文本过滤出来,

moive_people = ['sb_alex','sb_wupeiqi','yuanhao','sb_lihaifeng']

def filter_test(array):
ret = []
for p in array:
if not p.startswith("sb"):
ret.append(p)
return ret
print(filter_test(moive_people)) #['yuanhao']

2、将后面包含sb的文本过滤出来,如果还按照上面的方法写就显得很麻烦,而且代码重复,可以将for循环的主体部分使用函数定义独立出来,方便后续进行代码维护

 moive_people = ['alex_sb','sb_wupeiqi_sb','yuanhao','sb_lihaifeng_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
print(filter_test(sb_show,moive_people)) #['yuanhao']

备注:在上面的代码中,我将p.endswith()预先定义函数,然后后续直接调用函数,如果还有变化,只需要使用函数定义出来,然后直接调用;

3、对2的函数进行优化,将预先定义的sb_show函数使用lambda匿名函数进行替换,减少代码数量

 def filter_test(func,array):
ret = []
for p in array:
if not func(p):
ret.append(p)
return ret
print(filter_test(lambda n:n.endswith('sb'),moive_people))

4、filter函数

 moive_people = ['alex_sb','sb_wupeiqi_sb','yuanhao','sb_lihaifeng_sb']
#下面的结果是一个内存地址,如果要获取值,需要进行list
#<filter object at 0x0000026CB0A4FAC8>
print(filter(lambda n:n.endswith('sb'),moive_people))
#['alex_sb', 'sb_wupeiqi_sb', 'sb_lihaifeng_sb']
print(list(filter(lambda n:n.endswith('sb'),moive_people)))
#['alex_sb', 'sb_wupeiqi_sb', 'sb_lihaifeng_sb']
res= filter(lambda n:n.endswith('sb'),moive_people)
print(list(res))

二、reduce函数

reduce() 函数会对参数序列中元素进行累积。

函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果

1、采用for循环的方法进行实现

 num1 = range(1,101)
res= 0
for i in num1:
res = res+i print(res) #5050

2、使用函数的方式进行实现

 num1 = range(1,101)
def reduce_test(array): res= 0
for i in array:
res = res+i
return(res)
print(reduce_test(num1)) #

3、对序列中的每个元素进行乘积

 num1 = range(1,6)
def mul(x,y):
return x*y
def reduce_test(func,array):
res =1
for i in array:
res = func(res,i)
return res
print(reduce_test(mul,num1))

4、对3函数进行优化

 def reduce_test(func,array):
res =1
for i in array:
res = func(res,i)
return res
print(reduce_test(lambda x,y:x*y,num1))

5、对4函数进行优化,对函数增加一个默认参数

 num1 = [1,2,3,4,5,6]
# def mul(x,y):
# return x*y
def reduce_test(func,array,init=None):
if init is None:
res = array.pop(0)
else:
res= init
for i in array:
print(i)
res = func(res,i)
return res
print(reduce_test(lambda x,y:x*y,num1,100))

6、reduce函数

 from functools import reduce
num1 = [1,2,3,4,5,6] print(reduce(lambda x,y:x+y,num1,1)) #
print(reduce(lambda x,y:x*y,num1,10)) #
print(reduce(lambda x,y:x/y,num1,100)) #0.1388888888888889

三、map函数

对序列中元素进行for循环,然后对每个元素进行逻辑处理

1、对列表中的元素进行平方处理

 num1 = [1,2,3,4,5,6]
ret= []
for i in num1:
ret.append(i**2)
print(ret)

2、对列表中的元素进行自增加1处理

 num1 = [1,2,3,4,5,6]
ret= []
for i in num1:
ret.append(i+1)
print(ret)

3、使用函数方式进行处理

 num1 = [1,2,3,4,5,6]
def map_test(array):
res =[]
for i in array:
res.append(i+1)
return res
print(map_test(num1)) #[2, 3, 4, 5, 6, 7]

4、对3函数进行优化,将for循环的主体代码部分预先使用函数进行定义

 num1 = [1,2,3,4,5,6]
def reduce_one(x):
return x+1
def map_test(func,array):
res =[]
for i in array:
res.append(func(i))
return res
print(map_test(reduce_one,num1)) #[2, 3, 4, 5, 6, 7]

如果需求有其他变化,只需要对将函数定义出来,然后进行调用就可以了

5、对4函数进行优化,使用lambda函数进行代替

 num1 = [1,2,3,4,5,6]
# def reduce_one(x):
# return x+1
def map_test(func,array):
res =[]
for i in array:
res.append(func(i))
return res
print(map_test(lambda x:x+1,num1)) #[2, 3, 4, 5, 6, 7]

6、map函数

 num1 = [1,2,3,4,5,6]
print(map(lambda x:x+1,num1)) #<map object at 0x0000025C4059F908>
print(list(map(lambda x:x+1,num1))) #[2, 3, 4, 5, 6, 7]

---恢复内容结束---

一、filter函数

filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。

该接收两个参数,第一个为函数,第二个为序列,对序列中每个元素进行for循环,然后将每个元素传递给第一个位置的函数,然后返回 True 或 False,最后将返回 True 的元素放到新列表中

1、使用for循环将前面包含sb的文本过滤出来,

moive_people = ['sb_alex','sb_wupeiqi','yuanhao','sb_lihaifeng']

def filter_test(array):
ret = []
for p in array:
if not p.startswith("sb"):
ret.append(p)
return ret
print(filter_test(moive_people)) #['yuanhao']

2、将后面包含sb的文本过滤出来,如果还按照上面的方法写就显得很麻烦,而且代码重复,可以将for循环的主体部分使用函数定义独立出来,方便后续进行代码维护

 moive_people = ['alex_sb','sb_wupeiqi_sb','yuanhao','sb_lihaifeng_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
print(filter_test(sb_show,moive_people)) #['yuanhao']

备注:在上面的代码中,我将p.endswith()预先定义函数,然后后续直接调用函数,如果还有变化,只需要使用函数定义出来,然后直接调用;

3、对2的函数进行优化,将预先定义的sb_show函数使用lambda匿名函数进行替换,减少代码数量

 def filter_test(func,array):
ret = []
for p in array:
if not func(p):
ret.append(p)
return ret
print(filter_test(lambda n:n.endswith('sb'),moive_people))

4、filter函数

 moive_people = ['alex_sb','sb_wupeiqi_sb','yuanhao','sb_lihaifeng_sb']
#下面的结果是一个内存地址,如果要获取值,需要进行list
#<filter object at 0x0000026CB0A4FAC8>
print(filter(lambda n:n.endswith('sb'),moive_people))
#['alex_sb', 'sb_wupeiqi_sb', 'sb_lihaifeng_sb']
print(list(filter(lambda n:n.endswith('sb'),moive_people)))
#['alex_sb', 'sb_wupeiqi_sb', 'sb_lihaifeng_sb']
res= filter(lambda n:n.endswith('sb'),moive_people)
print(list(res))

二、reduce函数

reduce() 函数会对参数序列中元素进行累积。

函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果

1、采用for循环的方法进行实现

 num1 = range(1,101)
res= 0
for i in num1:
res = res+i print(res) #5050

2、使用函数的方式进行实现

 num1 = range(1,101)
def reduce_test(array): res= 0
for i in array:
res = res+i
return(res)
print(reduce_test(num1)) #

3、对序列中的每个元素进行乘积

 num1 = range(1,6)
def mul(x,y):
return x*y
def reduce_test(func,array):
res =1
for i in array:
res = func(res,i)
return res
print(reduce_test(mul,num1))

4、对3函数进行优化

 def reduce_test(func,array):
res =1
for i in array:
res = func(res,i)
return res
print(reduce_test(lambda x,y:x*y,num1))

5、对4函数进行优化,对函数增加一个默认参数

 num1 = [1,2,3,4,5,6]
# def mul(x,y):
# return x*y
def reduce_test(func,array,init=None):
if init is None:
res = array.pop(0)
else:
res= init
for i in array:
print(i)
res = func(res,i)
return res
print(reduce_test(lambda x,y:x*y,num1,100))

6、reduce函数

 from functools import reduce
num1 = [1,2,3,4,5,6] print(reduce(lambda x,y:x+y,num1,1)) #
print(reduce(lambda x,y:x*y,num1,10)) #
print(reduce(lambda x,y:x/y,num1,100)) #0.1388888888888889

三、map函数

对序列中元素进行for循环,然后对每个元素进行逻辑处理

1、对列表中的元素进行平方处理

 num1 = [1,2,3,4,5,6]
ret= []
for i in num1:
ret.append(i**2)
print(ret)

2、对列表中的元素进行自增加1处理

 num1 = [1,2,3,4,5,6]
ret= []
for i in num1:
ret.append(i+1)
print(ret)

3、使用函数方式进行处理

 num1 = [1,2,3,4,5,6]
def map_test(array):
res =[]
for i in array:
res.append(i+1)
return res
print(map_test(num1)) #[2, 3, 4, 5, 6, 7]

4、对3函数进行优化,将for循环的主体代码部分预先使用函数进行定义

 num1 = [1,2,3,4,5,6]
def reduce_one(x):
return x+1
def map_test(func,array):
res =[]
for i in array:
res.append(func(i))
return res
print(map_test(reduce_one,num1)) #[2, 3, 4, 5, 6, 7]

如果需求有其他变化,只需要对将函数定义出来,然后进行调用就可以了

5、对4函数进行优化,使用lambda函数进行代替

 num1 = [1,2,3,4,5,6]
# def reduce_one(x):
# return x+1
def map_test(func,array):
res =[]
for i in array:
res.append(func(i))
return res
print(map_test(lambda x:x+1,num1)) #[2, 3, 4, 5, 6, 7]

6、map函数

 num1 = [1,2,3,4,5,6]
print(map(lambda x:x+1,num1)) #<map object at 0x0000025C4059F908>
print(list(map(lambda x:x+1,num1))) #[2, 3, 4, 5, 6, 7]

四、其他内置函数

1、zip函数

zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。zip函数的作用类似于拉链

 print(list(zip(('张三'),(1,2))))#[('张', 1), ('三', 2)]
print(zip(('张三'),(1,2))) #<zip object at 0x00000230F8D54B88>
#[('M', 1), ('y', 2), (' ', 3)]
print(list(zip(('My name is zhangsan'),(1,2,3))))
print(list(zip(('My'),(1,2,3,4)))) #[('M', 1), ('y', 2)] ###使用zip函数将字典中的key与value值一一对应
p = {'name':'alex','age':18,'gender':'none'}
print(p.keys()) #dict_keys(['name', 'age', 'gender'])
print(p.values()) #dict_values(['alex', 18, 'none'])
print(list(p.keys())) #['name', 'age', 'gender']
print(list(p.values())) #['alex', 18, 'none']
#[('name', 'alex'), ('age', 18), ('gender', 'none')]
print(list(zip(p.keys(),p.values())))
print(zip(p.keys(),p.values())) #<zip object at 0x0000027B47A84F48>

2、max、min函数

(1)、单纯数字的比较

 l3 = [12,34,130,-1,44]
print(max(l3)) #
print(min(l3))#-1

(2)、字典的比较

 age_dict = {'age1':18,'age3':30,'age4':87}
#求出年龄最大的
print(max(age_dict.values())) #
#默认比较key值
print(max(age_dict))
#求出年龄最大的keys与values print(list(max(zip(age_dict.values(),age_dict.keys())))) #[87, 'age4']
 l = [
(1,'a'),
(2,'b'),
(3,'e'),
(5,'f')
]
print(max(l)) l1 = ['a10','b13','d13']
print(list(max(l1))) #['d', '1', '3'] # l2 = ['a10','b13','d13',10]
# #TypeError: '>' not supported between instances of 'int' and 'str'
# print(list(max(l2)))

3、chr、ord函数

 # print(chr(97)) #a,chr的作用是将数字在ascii码表中的值显示出来
# print(ord('c')) #99
# print(chr(33)) #! ascii码表中的33对应的值为'!'

4、pow函数

 print(pow(2,3))  #2**3
print(pow(2,3,2)) #2**3%2

5、reversed 反转

6、round四舍五入

7、set()转换为集合

8、slice() 切片

 l = 'hello'
print(l[2:5])
s= slice(3,5)
s1 = slice(1,4,2)
print(s)

9、sorted 排序

10、str

11、type

12、vars

13、__imprort__

14、eval

###将字符串中的结构给提取出来
str_1 = "1+2*(5-1)-20/5"
print(eval(str_1))
str_2 = "{'k1':'1234','k2':'张三'}"
print(eval(str_2))

15、hash

可hash的数据类型即为不可变数据类型,不可hash的数据类型即可变数据类型;

hash是一种算法、算法结构;

hash特性:

不管传入的参数多大,计算的hash值长度是不变的;

不能根据hash值去反推原参数;

变量不变hash值不变;

name = "zhangsan"
print(hash(name)) #-6633503532806232964
print(hash(name)) #-6633503532806232964
print(hash(name)) #-6633503532806232964
name = "张三"
print(hash(name)) #247632540932985384

16、bytes

17、encoding、decode

18、divmod

19、bin、hex、oct

print(bin(32)) #十进制转二进制
print(hex(32)) #十进制转16进制
print(oct(32)) #十进制转8进制
"""
输出结果
0b100000
0x20
0o40
"""

20、globals、local

												

python之函数filter、reduce的更多相关文章

  1. Python 内置模块函数filter reduce

    1.filter()实现过滤的功能 2.reduce()对序列中元素的连续操作可以通过循环来处理 3.map()对tuple元组进行解包操作,调用时设置map()的第一个参数为None 4.使用red ...

  2. Python中Lambda, filter, reduce and map 的区别

    Lambda, filter, reduce and map Lambda Operator Some like it, others hate it and many are afraid of t ...

  3. python基础--内置函数filter,reduce

    movie_people=["sb+_alex","sb_wupeiqi","han"] # def filter_test(array): ...

  4. Python中map,filter,reduce,zip的应用

    事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...

  5. Python高级函数--map/reduce

    名字开头大写 后面小写:练习: def normalize(name): return name[0].upper() + name[1:].lower() L1 = ['adam', 'LISA', ...

  6. Python高级函数--filter

    def is_palindrome(n): return str(n) == str(n)[::-1] #前两个‘:’表示整个范围,‘-’表示从后面,‘1’表示数据间隔 output = filter ...

  7. 【python】函数filter、map

  8. Python【map、reduce、filter】内置函数使用说明(转载)

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  9. Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted

    1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...

随机推荐

  1. 【随笔】CLR:.net的类型,内部到底长啥样?

    前言 一提到.net的类型,首当其冲的就是“引用类型”.“值类型”:我们在面试中,也会经常被问“来说说值类型和引用类型....”,这时候第一反应就是:“哎呀,这还不简单,值类型是传递的值的copy,值 ...

  2. vue.js环境在window和linux安装

    一.windows环境下安装vue 1.node.js安装:在node.js的官网上下载node的安装包 https://nodejs.org/en/download/ 安装完毕之后,在命令行下验证是 ...

  3. nodejs-翻转算法

    nodejs-翻转算法 /** * Created by moon on 2019/12/14. */ //程序运行完成时一定要有输出语句,本工具才能正确展示运行结果. function abc() ...

  4. Windows下实现应用程序看门狗软件

    有时候,我们需要确保一个应用程序长期稳定地运行,但是在Windows平台上由于种种原因,几乎不可能保障一个应用的绝对可靠运行.那么,我们就需要有一个机制,在应用死机﹑异常﹑系统重启等情况下自我恢复,而 ...

  5. JavaScript prototype原型用法

    JavaScript对象原型 所有JavaScript对象都从原型继承属性和方法. <!DOCTYPE html> <html> <meta charset=" ...

  6. flux架构的详细介绍和使用!

    结构分为四个 视图 view动作 action派发器 dispatcher数据商店 store 流程: 用户操作视图 视图(view)发送动作(action)到派发器(dispatcher) 由派发器 ...

  7. CTF必备技能丨Linux Pwn入门教程——利用漏洞获取libc

    Linux Pwn入门教程系列分享如约而至,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/am ...

  8. 华为手机 android8.0APP更新时出现安装包解析异常的提示及安装闪退(无反应)问题

    在做android app升级更新时遇到几个问题,我用的测试机是华为V10 系统为8.0 一.安装闪退(无反应) 解决办法: 只要在Mainfest.xml 中加入权限编码即可解决 <uses- ...

  9. Mybatis使用Mybatis-generator插件及配置(数据库逆向工程)

    Mybatis使用Mybatis-generator插件 首先在POM.xml文件添加架包,我这里用的是SpringBoot,所以用的也是SpringBoot架包,最少要mybatis,generat ...

  10. windows elasticsearch搭集群启动失败failed to send join request to master....

    创建几份elasticsearch副本,修改各自config\elasticsearch.yml配置文件: 第一份: #允许elasticsearch跨域访问,使用elasticsearch-head ...