filter,map,reduce,lambda(python3)
1.filter
filter(function,sequence)
对sequence中的item依次执行function(item),将执行的结果为True(符合函数判断)的item组成一个list、string、tuple(根据sequence类型决定)返回。
#!/usr/bin/env python
# encoding: utf-8
"""
@author: 侠之大者kamil
@file: filter.py
@time: 2016/4/9 22:03
"""
#filter map reduce lambda
def f1(x):
return x % 2 != 0 and x % 3 != 0
a = filter(f1, range(2,25))
print(a)#<filter object at 0x7f7ee44823c8>
#这种情况是因为在3.3里面,filter()的返回值已经不再是list,而是iterators.
print(list(a))
def f2(x):
return x != "a"
print(list(filter(f2,"dfsafdrea")))
结果:
ssh://kamil@192.168.16.128:22/usr/bin/python3 -u /home/kamil/py22/jiqiao0406/fmrl.py
<filter object at 0x7f4d1ca5e470>
[5, 7, 11, 13, 17, 19, 23]
['d', 'f', 's', 'f', 'd', 'r', 'e'] Process finished with exit code 0
2.map
语法与filter类似
#!/usr/bin/env python
# encoding: utf-8
"""
@author: 侠之大者kamil
@file: map.py
@time: 2016/4/9 22:22
"""
def cube(x):
return x * x *x
a = map(cube,range(10))
print(list(a))
def add(x,y,z):
return x + y +z
b = map(add,range(5),range(5),range(5))
print(list(b))
结果:
ssh://kamil@192.168.16.128:22/usr/bin/python3 -u /home/kamil/py22/jiqiao0406/map.py
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
[0, 3, 6, 9, 12] Process finished with exit code 0
3.reduce
reduce已经取消了,如想使用,可以用fuctools.reduce来调用。但是要先导入fuctools, 即输入:import fuctools
4.lambda
快速定义最小单行函数
g = lambda x:x *2
print(g(5))
print((lambda x:x *2)(5))
结果:
10
10
filter,map,reduce,lambda(python3)的更多相关文章
- Python2.7学习笔记-定义函数、filter/map/reduce/lambda
我把写的代码直接贴在下面了,注释的不是很仔细,主要是为了自己复习时方便查找,并不适合没有接触过python的人看,其实我也是初学者. #定义函数 def my_abs(x): if x>=0: ...
- Map、Filter和Reduce函数(Python)
Map map(function_to_apply, list_of_inputs) 设有以下代码: >>> items = [1, 2, 3, 4, 5] >>> ...
- Python中特殊函数和表达式 filter,map,reduce,lambda
1. filter 官方解释:filter(function or None, sequence) -> list, tuple, or string Return those items of ...
- 一步一步跟我学习hadoop(5)----hadoop Map/Reduce教程(2)
Map/Reduce用户界面 本节为用户採用框架要面对的各个环节提供了具体的描写叙述,旨在与帮助用户对实现.配置和调优进行具体的设置.然而,开发时候还是要相应着API进行相关操作. 首先我们须要了解M ...
- Python中 filter | map | reduce | lambda的用法
1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...
- python: filter, map, reduce, lambda
filter built-in function filter(f,sequence) filter can apply the function f to each element of seque ...
- Python学习(五)函数 —— 内置函数 lambda filter map reduce
Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...
- Python之匿名函数(filter,map,reduce)
参考博客:Python匿名函数详解--http://blog.csdn.net/csdnstudent/article/details/40112803 Python内建函数之——filter,map ...
- lambda,filter,map,reduce
# lambda,filter,map,reduce from functools import reduce print('返回一个迭代器') print((x) for x in range(5) ...
随机推荐
- Nuget自己打包引用的时候出现错误:Package is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package 1.0.1 supports: net (.NETFramework,Version=v0.0)
Nuget自己打包引用的时候出现错误:Package is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package ...
- AASM rule of scoring sleep stages using EEG signal
Reference: AASM (2007). The AASM Manual for the Scoring of Sleep and Associated Events: Rules, Termi ...
- LOG4NET日志配置及使用
Log4net的安装 Install-Package log4net 1.先弄个日志记录的类 /// <summary> /// 使用LOG4NET记录日志的功能,在WEB.CONFIG里 ...
- Java 集合系列06之 Vector详细介绍(源码解析)和使用示例
概要 学完ArrayList和LinkedList之后,我们接着学习Vector.学习方式还是和之前一样,先对Vector有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.第1部分 Vec ...
- SQL SERVER 系统库查询
本文内容主要来自网络,如有错误请路过的大牛指点迷津. 1.sqlserver 数据库最大并发连接数 sqlserver的最大连接数虽然说是不限制,但实际的限制数量是32767,如果需要超出这个数量,一 ...
- web standards
http://www.w3.org/standards/ http://www.webstandards.org/learn/faq/#p213 http://www.w3.org/standards ...
- 【转】Sql Server参数化查询之where in和like实现详解
转载至:http://www.cnblogs.com/lzrabbit/archive/2012/04/22/2465313.html 文章导读 拼SQL实现where in查询 使用CHARINDE ...
- QT 网络编程二(UDP版本)
QT的UdpSocket接收消息使用原则 第一步:new一个UdpSocket 第二步:调用UdpSocket的bind方法,同时指定端口号 第三步:使用connect将接收消息函数和UdpSocke ...
- JSP 和 ASP.NET 谁能主宰未来【转】
随着计算机行业的发展,以后到底谁才是 web 网站开发的主宰者呢? 1. 说说JSP.(本人工作中用的最多的就是JSP) JSP. JavaServer Pages 是Java技术的一部分,可以说是J ...
- JSON简介以及用法汇总
什么是JSON? JavaScript 对象表示法(JavaScript Object Notation). JSON是一种轻量级的数据交换格式,某个JSON格式的文件内部譬如可以长成这样: { &q ...