Python map及filter函数使用方法解析
作者:志不坚者智不达 时间:2021-11-20 05:25:03
知道python有这几个内置方法,但一直以来用的都不多,最近重新看了一下,重新记录一下。
map()会根据提供的函数对指定序列进行映射,python3会返回一个迭代器,具体用法如下:
def double(x):
return 2*x
if __name__=="__main__":
print(map(double,[1,2,3,4,5]))
print()
for i in map(double,[1,2,3,4,5]):
print(i)
运行结果:
F:\dev\python\python.exe F:/pyCharm/L02_Test/L02Interface/L02_Common/try_demo.py
<map object at 0x000002A3D91A3EF0>
2
4
6
8
10
Process finished with exit code 0
filter()内置函数用于过滤序列,用于过滤不符合条件的元素,返回符合条件的元素的列表,python3返回一个迭代器。
def is_odd(x):
return x%2==0
if __name__=="__main__":
print(filter(is_odd,[1,2,3,4,5,6,7,8,9,10]))
print()
for i in filter(is_odd,[1,2,3,4,5,6,7,8,9,10]):
print(i)
运行结果:
F:\dev\python\python.exe F:/pyCharm/L02_Test/L02Interface/L02_Common/try_demo.py
<filter object at 0x000001C75D243FD0>
2
4
6
8
10
Process finished with exit code 0
来源:https://www.cnblogs.com/linwenbin/p/11960143.html
标签:python,map,filter,函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python解析、提取url关键字的实例详解
2023-07-08 11:52:27
![](https://img.aspxhome.com/file/2023/4/76994_0s.jpg)
最常用的SQL语句
2024-01-23 20:37:40
如何使用pyinstaller打包32位的exe程序
2021-12-17 10:15:20
![](https://img.aspxhome.com/file/2023/2/86362_0s.png)
Pytorch数据类型与转换(torch.tensor,torch.FloatTensor)
2023-03-31 13:32:36
![](https://img.aspxhome.com/file/2023/0/63940_0s.png)
Python OS模块常用函数说明
2022-08-28 06:34:39
使用SQL语句,查第10-20条记录
2008-02-19 18:34:00
python频繁写入文件时提速的方法
2023-11-11 01:48:40
如何利用Python处理excel表格中的数据
2022-10-27 03:32:38
![](https://img.aspxhome.com/file/2023/6/118366_0s.png)
教学演示-UBB,剪贴板,textRange及其他
2008-01-27 13:46:00
Python程序设计入门(2)变量类型简介
2021-09-28 14:51:18
Python基于BeautifulSoup爬取京东商品信息
2021-03-15 21:52:53
![](https://img.aspxhome.com/file/2023/6/72256_0s.jpg)
Python数学建模学习模拟退火算法旅行商问题示例解析
2023-07-28 08:33:24
![](https://img.aspxhome.com/file/2023/4/115094_0s.png)
MySQL sql_mode的使用详解
2024-01-16 00:13:59
SQLServer三种开窗函数详细用法
2024-01-20 09:03:00
![](https://img.aspxhome.com/file/2023/9/86879_0s.png)
python 教程之blinker 信号库
2023-11-23 10:15:51
MySQL的双机热备份安装和配置
2012-01-05 19:00:58
用python画个敬业福字代码
2022-04-04 18:36:59
![](https://img.aspxhome.com/file/2023/4/115194_0s.png)
Python函数生成器原理及使用详解
2023-12-02 17:57:31
![](https://img.aspxhome.com/file/2023/1/130831_0s.png)
使用Python解析Chrome浏览器书签的示例
2023-03-12 01:14:34
MySQL修改配置,区分大小写
2010-12-03 16:31:00