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等)的更多相关文章

  1. Python中的map()函数和reduce()函数的用法

    Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下   Py ...

  2. Python之内建函数Map,Filter和Reduce

    Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...

  3. 闭包 -> map / floatMap / filter / reduce 浅析

    原创: 转载请注明出处 闭包是自包含的函数代码块,可以在代码中被传递和使用 闭包可以捕获和存储其所在上下文中任意常量和变量的引用.这就是所谓的闭合并包裹着这些常量和变量,俗称闭包.Swift 会为您管 ...

  4. python 链表表达式 map、filter易读版

    链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...

  5. python学习之map函数和reduce函数的运用

    MapReduce:面向大型集群的简化数据处理引文 map()函数 Python中的map()函数接收两个参数,一个是调用函数对象(python中处处皆对象,函数未实例前也可以当对象一样调用),另一个 ...

  6. python中的map、filter、reduce函数

    三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str.   1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...

  7. python中的map,filter,zip函数

    map() Return an iterator that applies function to every item of iterable, yielding the results 例如: a ...

  8. 简单易懂之python 中的map,filter,reduce用法

    map(function,sequence) 把sequence中的值当参数逐个传给function,返回一个包含函数执行结果的list. 重点是结果返回一个列表,这样对返回的列表就可以干很多的活了. ...

  9. 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)) ...

随机推荐

  1. flask学习(十二):for循环遍历

    一. 字典的遍历 语法和python一样,可以使用items().keys().values().iteritems().iterkeys().itervalues() {% for k, v in ...

  2. How to find per-process I/O statistics on Linux

    以下转自http://www.xaprb.com/blog/2009/08/23/how-to-find-per-process-io-statistics-on-linux/ Newer Linux ...

  3. RabbitMQ 如何保证消息不丢失?

    RabbitMQ一般情况很少丢失,但是不能排除意外,为了保证我们自己系统高可用,我们必须作出更好完善措施,保证系统的稳定性. 下面来介绍下,如何保证消息的绝对不丢失的问题,下面分享的绝对干货,都是在知 ...

  4. mbstring.so下载安装

    linux下安装: $:cd /php7.0/ext/mbstring 切换到源码包目录下 $:/usr/local/php/bin/phpize 执行这句 $:./configure –with-p ...

  5. debug调试日志和数据查询

    手动删除es文件并释放磁盘空间 1.停掉服务 systemctl stop xsdaemon.service 2.删掉索引 rm -rf /home/storager/c3dceb5e-bacc-4a ...

  6. shiro:10个过滤器;10个jsp标签;5个@注解

    10个过滤器 过滤器简称 对应的java类 anon org.apache.shiro.web.filter.authc.AnonymousFilter authc org.apache.shiro. ...

  7. CSS技巧和经验

    如何清除图片下方出现几像素的空白间隙 方法1 img { display: block; } 方法2 除了top值,还可以设置为text-top | middle | bottom | text-bo ...

  8. hdu 3032 Nim or not Nim? sg函数 难度:0

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. 使用Eclipse EE(汉化版) 创建一个JavaWeb工程

    第一步:打开eclipse ee,单击“文件”-->单击“新建”-->单击“动态Web项目”. 若没找到“动态Web项目”,单击“其他” -->在弹出的窗口中打开“Web”下拉菜单 ...

  10. Python3的第一个程序(三)

    现在,了解了如何启动和退出Python的交互式环境,我们就可以正式开始编写Python代码了. 在写代码之前,请千万不要用“复制”-“粘贴”把代码从页面粘贴到你自己的电脑上.写程序也讲究一个感觉,你需 ...