一、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. Java对象的内存布局以及对象的访问定位

    一 Java对象的内存布局 在HotSpot虚拟机中,对象在内存中的布局分为3个区域 对象头(Header) Mark Word(在32bit和64bit虚拟机上长度分别为32bit和64bit)存储 ...

  2. git分支提交管理

    随着需求的增多,为了多人协作的顺利进行,需要进行分支开发,进而带来分支管理问题.今天主要讲一下如何管理分支及提交. 为了使git更好用,下面是我的git配置文件(放在C:\Users\Administ ...

  3. ios 微信环境 axios请求 status 0

    做了一个支付页面,调用post请求但是请求status 0,出现这个的原因居然是https的网页请求http的数据. 但是这个再ios里面不会报错,安卓正常. 记录一下客户端的这个特征!

  4. 【经验总结】OSG 安装配置

    对于普通用户推荐直接下载安装包配置.如有特殊需求或想了解编译过程可参考网上文章自己编译后配置.(通常建议使用第一种方法即可) 本人安装经验: 失败:自己系统64位,VS2010 32位,开始自己动手编 ...

  5. laravel权限控制Gate

    实现思想 注册 位置: app/Providers/AuthServiceProvider.php $permissions = \App\AdminPermission::all(); foreac ...

  6. 【HEVC简介】CTU、CU、PU、TU结构

     参考文献:见<High Efficiency Video Coding (HEVC)>Block Structures and Parallelism Features in HEVC章 ...

  7. 获取父页面的dom元素

    $("li.jericho_tabs", window.top.document); 上面的代码意思是获取父页面的li元素,class为jericho_tabs的所有元素.

  8. 生成hprof文件,用MAT进行分析

    生成hprof文件可以在DDMS选中进程点击窗口左上角的"dump hprof file"按钮来直接生成,也可以通过在程序加代码中来生成 代码2: void generateHpr ...

  9. codevs 1519 过路费

    时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master 题目描述 Description 在某个遥远的国家里,有 n个城市.编号为 1,2,3,…,n.这个国家的政府 ...

  10. [Python學習筆記] 抓出msg信件檔裡的附件檔案

    想要把msg信件檔案的附件抓出來做處理,找到了這個Python 模組 msg-extractor 使用十分容易,但是這個模組是要在terminal裡執行,無法直接打在IDLE的編輯器上 所以稍微做了修 ...