python一些内建函数(map,zip,filter,reduce,yield等)
python一些内建函数(map,zip,filter,reduce,yield等)
map函数
Python实际上提供了一个内置的工具,map函数。这个函数的主要功能是对一个序列对象中的每一个元素应用被传入的函数,并且返回一个包含了所有函数调用结果的一个列表。
map?
Docstring:
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
Type: builtin_function_or_method
下面三个例子参考了该博文
对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
def add100(x):
return x+100
hh = [11,22,33]
map(add100,hh)
Out[2]:
[111, 122, 133]
如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用‘function’。
def abc(a, b, c):
return a*10000 + b*100 + c
list1 = [11,22,33]
list2 = [44,55,66]
list3 = [77,88,99]
map(abc,list1,list2,list3)
Out[3]:
[114477, 225588, 336699]
如果'function'给出的是‘None’,自动假定一个‘identity’函数(这个‘identity’不知道怎么解释,看例子吧)
list1 = [11,22,33]
map(None,list1)
Out[5]:
[11, 22, 33]
list1 = [11,22,33]
list2 = [44,55,66]
list3 = [77,88,99]
map(None,list1,list2,list3)
Out[6]:
[(11, 44, 77), (22, 55, 88), (33, 66, 99)]
zip函数
以下参考了博文
函式说明:zip(seq1[,seq2 [...]])->[(seq1(0),seq2(0)...,(...)]。 同时循环两个一样长的函数,返回一个包含每个参数元组对应元素的元组。若不一致,采取截取方式,使得返回的结果元组的长度为各参数元组长度最小值。
for x,y in zip([1,2,3],[4,5,6]):
print x,y
for x,y in zip([1,2,3],[4,5,6]):
print x,y
1 4
2 5
3 6
for x,y in zip([1,2,3],[5,6]):
print x,y
1 5
2 6
filter函数
filter(bool_func,seq):此函数的功能相当于过滤器。 调用一个布尔函数bool_func来迭代遍历每个seq中的元素,返回一个使bool_seq返回值为true的元素的序列。
filter(lambda x:x%2==0,[1,2,3,4,6,5,7])
Out[15]:
[2, 4, 6]
reduce函数
reduce(func,seq[,init]):func为二元函数,将func作用于seq序列的元素,每次携带一对(先前的结果以及下一个序列的元素),连续的将现有的结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值:如果初始值init给定,第一个比较会是init和第一个序列元素而不是序列的头两个元素。
reduce(lambda x,y:x+y,[1,2,3,4])
Out[22]:
10
reduce(lambda x,y: x-y,[1,2,3,4,5],19)
reduce(lambda x,y: x-y,[1,2,3,4,5],19)
Out[23]:
4
reduce(lambda x,y: x-y,[1,2,3,5],5)
Out[21]:
-6
isinstance
isinstance(object, classinfo) 判断一个对象是否是一个已知的类型。
isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
其第一个参数为对象,第二个为类型名或类型名的一个列表。其返回值为布尔型。若对象的类型与参数二的类型相同则返回True。若参数二为一个元组,则若对象类型与元组中类型名之一相同即返回True。
tes = list()
tes.append(3)
ta= 'a','b'
print tes
print isinstance(tes, list)
print isinstance(ta, tuple)
print isinstance(tes,(int, tuple, list))
print isinstance(tes,(int, tuple))
Out:
[3]
True
True
True
False
id
查看object 地址
in:
print id.__doc__
Out:
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory address.)
assert
断言
Python中assert用来判断语句的真假,如果为假的时候,将触发AssertionError错误
a = None
assert a != None
print a
out:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-1-4165ca7bb901> in <module>()
1
2 a = None
----> 3 assert a != None
4 print a
AssertionError:
另一例子:
a=[1,2,3]
# a = None
assert len(a) < 5 # 为真通过
assert a != None
print a
out:
[1, 2, 3]
min && max
c = [-10, -45, 0, 5, 3, 50, 15, -20, 25]
print "min: %s" %c.index(min(c)) # 返回最小值
print "max: %s" %c.index(max(c)) # 返回最大值
out:
min: 1
max: 5
python一些内建函数(map,zip,filter,reduce,yield等)的更多相关文章
- Python中的map()函数和reduce()函数的用法
Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下 Py ...
- Python之内建函数Map,Filter和Reduce
Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...
- 闭包 -> map / floatMap / filter / reduce 浅析
原创: 转载请注明出处 闭包是自包含的函数代码块,可以在代码中被传递和使用 闭包可以捕获和存储其所在上下文中任意常量和变量的引用.这就是所谓的闭合并包裹着这些常量和变量,俗称闭包.Swift 会为您管 ...
- python 链表表达式 map、filter易读版
链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...
- python学习之map函数和reduce函数的运用
MapReduce:面向大型集群的简化数据处理引文 map()函数 Python中的map()函数接收两个参数,一个是调用函数对象(python中处处皆对象,函数未实例前也可以当对象一样调用),另一个 ...
- python中的map、filter、reduce函数
三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str. 1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...
- python中的map,filter,zip函数
map() Return an iterator that applies function to every item of iterable, yielding the results 例如: a ...
- 简单易懂之python 中的map,filter,reduce用法
map(function,sequence) 把sequence中的值当参数逐个传给function,返回一个包含函数执行结果的list. 重点是结果返回一个列表,这样对返回的列表就可以干很多的活了. ...
- 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)) ...
随机推荐
- Java MongoDB插入
前言 插入是向MongoDB中添加数据的基本方法.对目标集使用insert方法来插入一条文档.这个方法会给文档增加一个”_id”属性(如果原来没有的话),然后保存到数据库中. 1.连接数据库,拿到集合 ...
- 1029: [JSOI2007]建筑抢修 贪心
https://www.lydsy.com/JudgeOnline/problem.php?id=1029 题意:n个建筑,每个有修复时间和爆炸时间,没有在爆炸时间内修复就会爆炸,问最多能修复的建筑 ...
- Highcharts 基本曲线图;Highcharts 带有数据标签曲线图表;Highcharts 异步加载数据曲线图表
Highcharts 基本曲线图 实例 文件名:highcharts_line_basic.htm <html> <head> <meta charset="U ...
- NOI Linux下Emacs && gdb调试方法
1. 首先要配置emacs文件: (global-linum-mode t) (show-paren-mode t) (global-set-key (kbd "C-s") 'sa ...
- Accesshelper.cs
using System; using System.Data; using System.Data.OleDb; using System.Collections; using System.IO; ...
- 2018-2019-2 网络对抗技术 20165210 Exp4 恶意代码分析
2018-2019-2 网络对抗技术 20165210 Exp4 恶意代码分析 一.实验目标 首先是监控你自己系统的运行状态,看有没有可疑的程序在运行. 其次是分析一个恶意软件,就分析Exp2或Exp ...
- 解析xml节点属性及子节点内容
xml样例 <microNearlyThreeYearsOverdueInfo subReportType="13204" subReportTypeCost="9 ...
- jquery设置控件位置的方法
纯JS写法,代码如下: document.getElementById("child").style.left="800px";document.getElem ...
- Kotlin Reference (三) Coding Conventions
most from reference 命名规则 1.使用驼峰式命名规则,尽量避免在命名中使用下划线 2.类型以大写字母开头 3.方法和属性以小写字母开头 4.使用4个空格缩进 5.public的方法 ...
- pom配置之:snapshot快照库和release发布库
在使用maven过程中,我们在开发阶段经常性的会有很多公共库处于不稳定状态,随时需要修改并发布,可能一天就要发布一次,遇到bug时,甚至一天要发布N次.我们知道,maven的依赖管理是基于版本管理的, ...