zoukankan      html  css  js  c++  java
  • 内置函数--map,filter,reduce

    一、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
    View Code
    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
    View Code

    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]
    
  • 相关阅读:
    Java实现LeetCode_0028_ImplementStrStr
    Java实现图形化计算器
    Java实现图形化计算器
    Java实现图形化计算器
    Java实现图形化计算器
    Java实现LeetCode_0026_RemoveDuplicatesFromSortedArray
    Java实现LeetCode_0026_RemoveDuplicatesFromSortedArray
    Java实现LeetCode_0026_RemoveDuplicatesFromSortedArray
    Java实现LeetCode_0026_RemoveDuplicatesFromSortedArray
    STS开发环境搭建与配置
  • 原文地址:https://www.cnblogs.com/chenlin163/p/7261197.html
Copyright © 2011-2022 走看看