美文网首页
Python基础(8)——内建函数

Python基础(8)——内建函数

作者: _羊羽_ | 来源:发表于2018-07-16 00:55 被阅读754次

    定义

    Build-in Function,启动python解释器,输入dir(builtins), 可以看到很多python解释器启动后默认加载的属性和函数,这些函数称之为内建函数, 这些函数因为在编程时使用较多,cpython解释器用c语言实现了这些函数,启动解释器 时默认加载。由于内建函数数量众多,可以通过help(function)查看函数的使用方法。

    range

        range(stop) -> list of integers
        range(start, stop[, step]) -> list of integers
    

    start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);
    stop:到stop结束,但不包括stop.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
    step:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
    python2中range返回列表,python3中range返回一个迭代值。如果想得到列表,可通过list函数

    a = range(5)
    list(a)
    

    创建列表的另外一种方法

    li = [x+3 for x in range(5)]
    print(li)
    # [3, 4, 5, 6, 7]
    

    map

    map函数会根据提供的函数对指定序列做映射

        map(...)
            map(function, sequence[, sequence, ...]) -> list
    

    function:是一个函数
    sequence:是一个或多个序列,取决于function需要几个参数
    返回值是一个list
    参数序列中的每一个元素分别调用function函数,返回包含每次function函数返回值的list。
    函数需要一个参数

    map(lambda x: x*x, [1, 2, 3])
    # 结果为:[1, 4, 9]
    

    函数需要两个参数

    map(lambda x, y: x+y, [1, 2, 3], [4, 5, 6])
    # 结果为:[5, 7, 9]
    
    def f1( x, y ):  
        return (x,y)
    
    l1 = [ 0, 1, 2, 3, 4, 5, 6 ]  
    l2 = [ 'Sun', 'M', 'T', 'W', 'T', 'F', 'S' ]
    l3 = map( f1, l1, l2 ) 
    print(list(l3))
    #结果为:[(0, 'Sun'), (1, 'M'), (2, 'T'), (3, 'W'), (4, 'T'), (5, 'F'), (6, 'S')]
    

    filter过滤函数

    filter函数会对指定序列执行过滤操作

    filter(...)
        filter(function or None, sequence) -> list, tuple, or string
    
        Return those items of sequence for which function(item) is true.  If
        function is None, return the items that are true.  If sequence is a tuple
        or string, return the same type, else return a list.
    function:接受一个参数,返回布尔值True或False
    sequence:序列可以是str,tuple,list
    filter函数会对序列参数sequence中的每个元素调用function函数,最后返回的结果包含调用结果为True的元素。
    

    返回值的类型和参数sequence的类型相同

    from random import randint
    
    
    result= [randint(-10,10) for _ in range(10)]
    print(result)
    result = filter(lambda x:x>=0,result)
    print(list(result))
    

    注意在Python3以上filter的结果是filter object需要通过list转换成list

    reduce

    reduce函数,reduce函数会对参数序列中元素进行累积

    reduce(...)
    reduce(function, sequence[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence,
    from left to right, so as to reduce the sequence to a single value.
    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
    of the sequence in the calculation, and serves as a default when the
    sequence is empty.
    

    function:该函数有两个参数
    sequence:序列可以是str,tuple,list
    initial:固定初始值
    reduce依次从sequence中取一个元素,和上一次调用function的结果做参数再次调用function。 第一次调用function时,如果提供initial参数,会以sequence中的第一个元素和initial 作为参数调用function,否则会以序列sequence中的前两个元素做参数调用function。 注意function函数不能为None。

    reduce(lambda x, y: x+y, [1,2,3,4])
    10
    
    reduce(lambda x, y: x+y, [1,2,3,4], 5)
    15
    
    reduce(lambda x, y: x+y, ['aa', 'bb', 'cc'], 'dd')
    'ddaabbcc'
    在Python3里,reduce函数已经被从全局名字空间里移除了, 它现在被放置在fucntools模块里用的话要先引入: from functools import reduce
    

    sorted排序

    s = sorted([1, 6, 3, 2, 5, 4])
    print(s)
    #[1, 2, 3, 4, 5, 6]
    

    相关文章

      网友评论

          本文标题:Python基础(8)——内建函数

          本文链接:https://www.haomeiwen.com/subject/kjokqftx.html