一、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. PopupWindow(3)back,home 键无法关闭popupwindow的解决方案

    private PopupWindow mPopupWindow; //popup window 一般popuowindow 要都个显示view,本例子中view模拟菜单. private View ...

  2. vmware虚拟机启动centOs黑屏

    如图所示  , 我的VM 启动虚拟机之后就变成了上面的样子,一直不动,ping也ping不好,这个时候 : 1. 要么 内存不够了: 2. 要么 网络协议存在问题了: 本地windows环境在管理员的 ...

  3. Eclipse安装svn插件的几种方式 -- 转

    1.在线安装: (1).点击 Help --> Install New Software... (2).在弹出的窗口中点击add按钮,输入Name(任意)和Location(插件的URL),点击 ...

  4. html img标签显示一个默认图片

    1. [代码]img标签src对应的图片不存在,显示一个默认的图片 <img src="abc.JPG" onerror="this.src='default.JP ...

  5. CentOS 7.2安装pip

    CentOS 7.2默认安装的python版本为python2.7.5,我的系统里面默认是没有安装pip 的,搜了下网上各路大侠的解决办法,如下: 使用yum安装python-pip,但是报错,说没有 ...

  6. R 关于全局变量

    不得不吐槽了 写了这么多,竟然今天才发现R的全局变量在函数名空间里是不能赋值的,我去!!! 就是说在函数里面,全局变量名是可读的,但不可写(写的时候 又会创建新的 自由变量了)

  7. #219. 【NOI2016】优秀的拆分

      如果一个字符串可以被拆分为 AABBAABB 的形式,其中 AA 和 BB 是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 aabaabaa,如果令 A=aabA=aab ...

  8. HTTPS时代已来,你做好准备了吗?

    早在今年年初,Google在其安全博客上已经表明,从7月开始,Chrome68会将所有的HTTP网站标记为不安全.随后,Mozilla也表明,Firefox浏览器也准备将所有HTTP网站标记为不安全. ...

  9. Tcl介绍和基础语法

    Tcl的背景 Tcl(读作tickle)诞生于80年代的加州大学伯克利分校,作为一种简单高效可移植性好的脚本语言,目前已经广泛应用在几乎所有的EDA工具中.Tcl 的最大特点就是其语法格式极其简单,采 ...

  10. Linux常用终端快捷键

    UNIX程序员对键盘以及快捷键的设置都遵循一个标准:"手移动最少的距离,作更多的操作." 所有的类UNIX的终端上都有一些快捷键Ctrl+n = 下,Ctrl+b = 左,Ctrl ...