一、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. PHP常见的输出语句 echo()、print()、print_r()、var_dump()、pinrtf()之间的区别

    echo().print().print_r().var_dump().pinrtf()之间的区别 1.print() :简单输出语句,可以输出字符串,变量.表达式等单项的值!(不能输出数组.对象等复 ...

  2. YII2修改backend模块报错An Error occurred while handling another error: exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "site/error".' in E:\project\demo\vendor\yiisoft

    报错内容: 原因:没有修改common/config/bootstrap.php里的别名 修改后:

  3. vue_resource 使用说明

    前几天用vue-resource调用接口,用post方式给后端,发现后端php接受不到数据,这好奇怪,最后发现提交给后端的时候 需要加一个参数 就是:emulateJSON : true 这句话的意思 ...

  4. css实现行内文字垂直居中

    之前本人一直使用浮动.相对定位.绝对定位和display:table等css的方法进行定位.网上得知flex可实现弹性布局,符合未来发展趋势,随尝试. 1:让盒子行内文字垂直居中,解决思路是讲文字的行 ...

  5. IDEA下MyBatis错误总结

    1. Pom.xml配置 语法顺序 <properties resource="config.properties"> </properties> < ...

  6. spring mvc 解决 Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 异常

    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...

  7. centos开机直接进入命令行

    找到文件,/etc/inittab 文件,在下面有一行 id:5:initdefault: 将上面的5改成3就可以了 5是图形界面 3是命令行界面 就是文本界面.

  8. Servlet 3.0 新特性详解 (转载)

    原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-servlet30/ Servlet 3.0 新特性概述 Servlet 3.0 作为 Jav ...

  9. 升级或者重装Discuz! 版本后 QQ互联英文乱码显示的正确解决方法

    升级Discuz! X3版本QQ互联英文乱码!connect_viewthread_share_to_qq!  目前Discuz!论坛上 最简单的解决方法: 第一步:后台----->站长---- ...

  10. 用python+pygame写贪吃蛇小游戏

    因为python语法简单好上手,前两天在想能不能用python写个小游戏出来,就上网搜了一下发现了pygame这个写2D游戏的库.了解了两天再参考了一些资料就开始写贪吃蛇这个小游戏. 毕竟最开始的练手 ...