filter()函数 map()函数
来源:互联网 发布:c语言utf8转unicode 编辑:程序博客网 时间:2024/06/07 17:36
map(func, *iterables)两个参数,( 函数,序列(可以多个) )
map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回
filter(function or None, iterable) 两个参数 (一个函数,一个列表(元组,集合等))
里边function 对每个元素进行判断,返回 True或 False。filter()根据判断结果自动过滤掉不符合条件的元素,返回由符合条件元素组成的新列表等。
map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回
使用map求列表平方
L=[2,3,4,5]def fun(x): return x**2a=map(fun,L)print(a)print(list(a))输出:<map object at 0x00000000027CC5F8>---------输出生成列表内存地址[4, 9, 16, 25]
name=['alex','wupeiqi','yuanhao','nezha']print(name)print(list(map(lambda x:x+"_sb",name)))输出:['alex', 'wupeiqi', 'yuanhao', 'nezha']['alex_sb', 'wupeiqi_sb', 'yuanhao_sb', 'nezha_sb']
filter(function or None, iterable) 两个参数 (一个函数,一个列表(元组,集合等))
里边function 对每个元素进行判断,返回 True或 False。filter()根据判断结果自动过滤掉不符合条件的元素,返回由符合条件元素组成的新列表等。
用filter函数处理数字列表,将列表中所有的偶数筛选出来:
num = [1,3,5,6,7,8]def func(x): return x%2==0a = filter(func,num)print(a)print(list(a))可以优化:print(list(filter(lambda x: x%2==0,num)))输出:<filter object at 0x0000000001EACB38>[6, 8][6, 8]
阅读全文
0 0
- filter()函数 map()函数
- map、filter、reduce函数
- python-map()函数,reduce()函数,filter()函数
- python map函数,Filter函数,Reduce函数
- map、reduce、filter内置函数
- swift map filter reduce函数
- python map函数,filter,reduce
- python map, reduce, filter 函数
- filter,map,reduce,apply函数
- Map Filter和Reduce函数
- map,filter函数,评价统计
- Python3.4中filter函数,map函数和reduce函数
- Python3.4中filter函数,map函数和reduce函数
- Python3.4中filter函数,map函数和reduce函数
- Python的map、filter、reduce函数
- Python的map、filter、reduce函数
- python内置函数:lambda、map、filter
- [Python] 函数lambda(), filter(), map(), reduce()
- Regular polygon
- 设计模式3-抽象工厂模式
- 安卓城市列表
- c语言 linux下文件i/o 实现文件的复制
- STM32F107——OTG模块之USB设备之虚拟串口移植(一)
- filter()函数 map()函数
- 函数
- Nodejs 和npm 的关系
- 45. Jump Game II
- jquery的ajax请求data参数
- php里面CGI(common gateway interface),fast-cgi,php-fpm
- Linux命令分析: chgrp
- CodeForces
- MUI 代码键总结