python学习-29 map函数-filter函数
movie_person = ['小红','小明','小王','富豪_sb','美女_sb'] def filter_test(array):
ret = []
for i in array:
if not i.startswith('小'): # 以‘小’开头的
ret.append(i)
return ret
print(filter_test(movie_person)) def filter_test(array):
res = []
for i in array:
if not i.endswith('sb'): # 以‘sb’结尾的
res.append(i)
return res
print(filter_test(movie_person))
运行结果:
['富豪_sb', '美女_sb']
['小红', '小明', '小王'] Process finished with exit code 0
或另一种简单的方法:
movie_person = ['小红','小明','小王','富豪_sb','美女_sb'] def filter_test(func,array):
ret = []
for i in array:
if not func(i):
ret.append(i)
return ret res = filter_test(lambda x:x.endswith('sb'),movie_person) # 以‘sb’结尾的
print(res)
运行结果:
['小红', '小明', '小王'] Process finished with exit code 0
reduce函数
1.加法和乘法(两种方法)
from functools import reduce # 调用 reduce函数 num_1 = [1,2,3,4,5,6,7,8,9,100] def reduce_test(array):
res = 0
for num in array:
res += num
return res
print(reduce_test(num_1)) 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_1)) # 用lambda 进行乘法运算
运行结果:
145
36288000 Process finished with exit code 0
2.传一个初始值
from functools import reduce # 调用 reduce函数 num_1 = [1,2,3,4,5,6,7,8,9,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_1,100)) # 传了一个初始值100,以100开始乘以列表里的每个数
运行结果:
3628800000 Process finished with exit code 0
python学习-29 map函数-filter函数的更多相关文章
- Python 有用的 map() deduce() filter() 函数
#!/usr/bin/python#5!+4!+3!+2!+1! #give 3 return 3*2*1def jiechen(n): N = map(lambda x:x+1,range(n)) ...
- python中的map、filter、reduce函数
三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str. 1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...
- python学习三十九天filter() map()用法及lambda搭配使用
python函数中的 filter() map() 前者是过滤的,后者是映射关系,需要与函数搭配使用,这时候匿名函数派上用场了,用简单的表达式就可以显示比较复杂的功能 1,python函数 filte ...
- python一些内建函数(map,zip,filter,reduce,yield等)
python一些内建函数(map,zip,filter,reduce,yield等) map函数 Python实际上提供了一个内置的工具,map函数.这个函数的主要功能是对一个序列对象中的每一个元素应 ...
- Python学习十四:filter()
Python 中内置了filter()函数用于过滤序列. 使用方法: filter()接收一个函数和一个序列. filter()把传入的函数依次作用于每一个元素,然后依据返回值是True还是False ...
- Python学习 Day 5 高阶函数 map/reduce filter sorter 返回函数 匿名函数 装饰器 偏函数
高阶函数Higher-orderfunction 变量可以指向函数 >>> abs #abs(-10)是函数调用,而abs是函数本身 <built-in function ab ...
- Python学习(七)——匿名函数、map函数、filter函数、reduce函数与其他内置函数
匿名函数 lambda x: x + 1 # lambda:定义匿名函数的关键字 # x:形参 # x+1:程序处理逻辑 fun = lambda x: x + 1 print(fun(5)) #6 ...
- Python学习笔记系列——高阶函数(filter/sorted)
一.filter #filter()函数用于过滤序列.和map()类似,也接收一个函数和一个序列,把函数依次作用于每个元素,根据返回值是True还是False决定是否保留该元素. #filter()函 ...
- python中lambda,map,reduce,filter,zip函数
函数式编程 函数式编程(Functional Programming)或者函数程序设计,又称泛函编程,是一种编程范型,它将计算机运算视为数学上的函数计算,并且避免使用程序状态以及易变对象.简单来讲,函 ...
随机推荐
- Visual Studio 调试技巧---指针,元素个数
刚才,我在Visual Studio 中发现了一个以更好的方式调试指针的技巧.您可以在监视窗口中选择“n”,其中“n”是要显示的元素数.我认为下图是不言而喻的.
- string类的用法总结
string中常见的成员函数 示例代码: string s= string("abcdefg"); char ch[] = "abcdefgd"; //调用构造 ...
- 【AtCoder】 ARC 098
link C-Attention 题意:一个字符队列,每个位置是\(W\)或\(E\),计算最小的修改数量,使得存在一个位置,它之前的都是\(E\),之后的都是\(F\) #include<bi ...
- Linux:搭建GitLab
0.写在前面 GitLab官方明确要求最低配置2核4G,如果配置过低,会出现502错误. 1.安装SSH #安装ssh sudo yum install -y curl policycoreutils ...
- Spring中@Bean与@Configuration
一.Spring中Bean及@Bean的理解[1] Bean在Spring和SpringMVC中无所不在,将这个概念内化很重要,下面分享一下我的想法: 一.Bean是啥 1.Java面向对象,对象有方 ...
- 每天一个命令-cp 命令
cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录.它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下.cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文 ...
- Python80个练手项目列表
原文地址:https://www.shiyanlou.com/questions/102676/?utm_source=baidu&utm_medium=cpc&utm_campaig ...
- tensorflow keras analysis
目录 Q: where is Sequential defined? Q: where is compile()? tensorflow keras analysis code from keras. ...
- Kafka安装教程(详细过程)
安装前期准备: 1,准备三个节点(根据自己需求决定) 2,三个节点上安装好zookeeper(也可以使用kafka自带的zookeeper) 3,关闭防火墙 chkconfig iptables o ...
- 小程序 图表 antv f2 的使用
官方组件版 https://github.com/antvis/wx-f2/tree/custom-components 官方npm版 https://github.com/antvis/wx-f2 ...