一、map

class map(object):
"""
map(func, *iterables) --> map object Make an iterator that computes the function using arguments from
each of the iterables. Stops when the shortest iterable is exhausted.
"""
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass def __init__(self, func, *iterables): # real signature unknown; restored from __doc__
pass def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass @staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass def __next__(self, *args, **kwargs): # real signature unknown
""" Implement next(self). """
pass def __reduce__(self, *args, **kwargs): # real signature unknown
""" Return state information for pickling. """
pass
map(func, *iterables) --> map object
  Make an iterator that computes the function using arguments from
  each of the iterables. Stops when the shortest iterable is exhausted.
  """

  使用从参数来计算函数的迭代器每个迭代项。在最短的迭代结束时停止。

遍历序列,对序列中每个元素进行操作,最终获取新的序列。

给li = [1,2,3,4]每个元素加100

#!/usr/bin/python3

li = [1,2,3,4]

newList = map(lambda x:x+100, li)
print(newList)
print(list(newList))

执行结果:

<map object at 0x00000000027C4C88>
[101, 102, 103, 104]

参数传入自定义函数:

#!/usr/bin/python3

li = [1,2,3,4]

def myAdd (x):
return x * 100 newList = map(myAdd, li)
print(newList)
print(list(newList))

执行结果:

<map object at 0x00000000022249B0>
[100, 200, 300, 400]

二、filter

class filter(object):
"""
filter(function or None, iterable) --> filter object Return an iterator yielding those items of iterable for which function(item)
is true. If function is None, return the items that are true.
"""
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass def __init__(self, function_or_None, iterable): # real signature unknown; restored from __doc__
pass def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass @staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass def __next__(self, *args, **kwargs): # real signature unknown
""" Implement next(self). """
pass def __reduce__(self, *args, **kwargs): # real signature unknown
""" Return state information for pickling. """
pass

filter(function or None, iterable) --> filter object

  Return an iterator yielding those items of iterable for which function(item)
  is true. If function is None, return the items that are true.
  """
  返回一个迭代器,该迭代器可以为该函数(项)进行迭代是真的。如果函数是None,返回true的项。

对于序列中的元素进行筛选,最终获取符合条件的序列

#filter第一个参数为空,将获取原来序列

#!/usr/bin/python3

li = [1,2,3,4,5,6,7,8,9,10]

def myAdd (x):
return x % 2 == 0 newList = filter(myAdd, li)
print(newList)
print(list(newList)) print("---------------")
newList = filter(lambda x: x % 2 , li)
print(newList)
print(list(newList)) print("---------------")
#filter第一个参数为空,将获取原来序列
newList = filter(None, li)
print(newList)
print(list(newList))

执行结果:

<filter object at 0x0000000002824EB8>
[2, 4, 6, 8, 10]
---------------
<filter object at 0x000000000282B320>
[1, 3, 5, 7, 9]
---------------
<filter object at 0x0000000002824EB8>
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

内置函数--map,filter,reduce的更多相关文章

  1. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  2. python之内置函数:map ,filter ,reduce总结

    map函数: #处理序列中的每个元素,得到的结果是一个'列表',该列表元素个数及位置与原来一样 filter函数: #遍历序列中的每个元素,判断每个元素得到一个布尔值,如果是true,则留下来 peo ...

  3. [python基础知识]python内置函数map/reduce/filter

    python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...

  4. Python内置函数之filter map reduce

    Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...

  5. python之有用的3个内置函数(filter/map/reduce)

    这三个内置函数还是非常有用的,在工作中用的还不少,顺手,下面一一进行介绍 1.filter 语法:filter(function,iterable) 解释:把迭代器通过function函数进行过滤出想 ...

  6. Python学习(五)函数 —— 内置函数 lambda filter map reduce

    Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...

  7. python内置函数map/reduce/filter

    python有几个内置的函数很有意 思:map/filter/reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是python列表方法的三架 ...

  8. 内置函数_map()、reduce()、filter()

    map().reduce().filter() map()内置函数把一个函数func依次映射到序列或迭代器对象的每个元素上,并返回一个可迭代的map对象作为结果,map对象中每个元素是原序列中元素经过 ...

  9. 内置函数: filter 和 map

    内置函数———filter和map filter filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表.接收两个参数,第一个为函数,第二个为序列,序列的每个元素作 ...

  10. 高阶函数map(),filter(),reduce()

    接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions. map()和filter()是内置函数.在python3中,reduce()已不再是 ...

随机推荐

  1. 18.3.1获得Class对象

    package d18_3_1; /** * Java中的java.lang.Class,简单理解就是为每个java对象的类型标识的类, * 虚拟机使用运行时类型信息选择正确的执行方法,用来保存这些运 ...

  2. ubuntu16.04里如何正确添加用root用户来登录图形界面(图文详解)

    不多说,直接上干货! Ubuntu版本都默认不允许使用root登录,必须要改配置文件. 第一步: 首先设置root密码,利用现有管理员帐户登陆Ubuntu,在终端执行命令:sudo passwd ro ...

  3. SQL 列拼接使用

    一个产品收藏表 Collection , 把该产品被收藏的人拼接在一列中如下: SQL SERVER SELECT ProjectID, UserIDs = ','+(STUFF((SELECT ', ...

  4. window服务 调试步骤

    方法一: 1.编译windows服务项目工程 2.把服务注册到系统服务上 3.在visual studio 编辑器中,打断点,用 Debug  进程调试 方法二: 在Onstart 方法中,加上 De ...

  5. [转]java注解与APT技术

    下面是一个简单的自定义注解的栗子: package annotation; import java.lang.annotation.Documented; import java.lang.annot ...

  6. 【转】java节省内存的几条建议

    下面是参考网络资源总结的一些在Java编程中尽可能要做到的一些地方. 1. 尽量在合适的场合使用单例   使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单 ...

  7. spoj GCJ1C09C Bribe the Prisoners

    题目链接: http://www.spoj.com/problems/GCJ1C09C/ 题意: In a kingdom there are prison cells (numbered 1 to  ...

  8. ubuntu下php安装目录说明

    php当前安装目录 /etc/php5/ apache2:   采用APACHE2HANDLER启动 cli:   采用命令启动 fpm php-fpm启动 fpm2     php-fpm多实例 m ...

  9. 总结vue2.0 配置的实例方法

    总结vue2.0 配置的实例方法 http://www.php.cn/js-tutorial-369603.html

  10. 一条HTTP的生命之旅(高频面试问题)

    当你在浏览器地址栏输入一个URL后回车,将会发生的事情? 原文:http://igoro.com/archive/what-really-happens-when-you-navigate-to-a- ...