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. Java SHA256/Base64转.NET(C#)实现---(华为云云市场.NET版本加密方式)

    前言: 工作需要,对接华为云应用市场的 API 接口,由于维护团队都是 .NET 所以用 .NET 来开发. 简单了解一下 SHA256 加密算法,本质就是一个 Hash,与 MD5 相比就是计算量大 ...

  2. Python之路,Day9 - 线程、进程、协程和IO多路复用

    参考博客: 线程.进程.协程: http://www.cnblogs.com/wupeiqi/articles/5040827.html http://www.cnblogs.com/alex3714 ...

  3. Angular开发实践(七): 跨平台操作DOM及渲染器Renderer2

    在<Angular开发实践(六):服务端渲染>这篇文章的最后,我们也提到了在服务端渲染中需要牢记的几件事件,其中就包括不要使用window. document. navigator等浏览器 ...

  4. C#运算符笔记

    C# 原来也可以进行向量运算,这里解决了一个为时已久的疑惑. operator struct Vector { public double x, y, z; public Vector(double ...

  5. Winform 导航菜单的方法

    http://blog.163.com/kunkun0921@126/blog/static/169204332201171610619611/ 第一种:使用OutlookBar第三方控件 第二种:使 ...

  6. gitblit-部署

    什么是 Gitblit Gitblit是一个开源的用于管理,查看和提供Git仓库. 它主要设计为希望托管集中存储库的小工作组的工具. Gitblit有什么特点 Gitblit部署示例1 日常维护添加步 ...

  7. 一、html <!doctype>标签

    一.html <!doctype>标签 定义和用法 <!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. <!DOCTYP ...

  8. jQuery 选项卡 CleverTabs

    CleverTabs是一款jQuery插件,所需jQuery版本:1.6.1,jQuery UI样式版本:1.8.13:其功能为创建jQuery UI风格的Tab用于显示iframe. 本示例中符加了 ...

  9. SpringMVC札集(10)——SSM框架整合

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  10. select2切换事件如何生效

    1.问题背景 利用select2生成可搜索下拉框,并且绑定切换事件:但是直接绑定change事件,发现不起作用 2.问题原因 <!DOCTYPE html> <html> &l ...