6.3、高阶函数、常用内置函数


高阶函数:

  • 允许将函数作为参数传入另一个函数;
  • 允许返回一个函数。
#返回值为函数的函数
sum=lambda x,y:x+y
sub=lambda x,y:x-y
calc_dict={"+":sum,"-":sub}
def calc(x):
    return calc_dict[x]

print(calc('-')(5,6))
print(calc('+')(5,6))

#参数有函数的函数
filter(lambda x:x>5,range(20))

常用内置函数:

  • abs(x):求绝对值
  • range([start], stop[, step]) :产生一个序列,默认从0开始
    • 注意:返回的不是一个list对象
>>> print(range(20))
range(0, 20)
>>> type(range(20))
<class 'range'>
>>> isinstance(range(20),Iterable)#########是一个可迭代对象
True
>>> from collections import Iterator
>>> isinstance(range(20),Iterator)#不是一个迭代器对象
False
  • oct(x)
    将一个数字转化为8进制
  • hex(x)
    将整数x转换为16进制字符串
  • bin(x)
    将整数x转换为二进制字符串
>>> oct(8)
'0o10'
>>> hex(8)
'0x8'
>>> bin(8)
'0b1000'
  • chr(i):返回整数i对应的Unicode字符
  • ord(x):将字符转换成对应的Unicode编址
>>> ord('')
20013
>>> chr(20013)
''
  • enumerate(sequence [, start = 0]):返回一个可枚举的对象,该对象的next()方法将返回一个tuple
for i, value in enumerate(['A', 'B', 'C']):
    print(i, value)
  • iter(o[, sentinel])  :生成一个对象的迭代器,第二个参数表示分隔符
from collections import Iterator
#可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator。
print(isinstance([],Iterator))
print(isinstance(iter([]),Iterator))
    • sorted(iterable[, cmp[, key[, reverse]]])  对可迭代对象进行排序
    >>> l=[8,7,6,5,4,3,2,1]
    >>> sorted(l)
    [1, 2, 3, 4, 5, 6, 7, 8]
      • cmp(x, y)  :如果x < y ,返回负数;x == y, 返回0;x > y,返回正数
        • all(iterable)
          1、可迭代对象中的元素都为真的时候为真
          2、特别的,可迭代对象若为空返回为True
        >>> l=[]
        >>> all(l)
        True
        >>> l=[1,2,3,4,5]
        >>> all(l)
        True
        >>> l=[1,2,3,4,5,0]
        >>> all(l)
        False
          • any(iterable)
            1、可迭代对象中的元素有一个为真的时候为真
            2、特别的,可迭代对象若为空返回为False
          >>> l=[]
          >>> any(l)
          False
          >>> l=[0,0,0,0]
          >>> any(l)
          False
          >>> l=[0,0,0,0,5]
          >>> any(l)
          True
          >>>
          • eval(expression [, globals [, locals]])  :计算表达式expression的值
          >>> str1="3+4"
          >>> eval(str1)
          7
          • exec(object[, globals[, locals]]):执行储存在字符串或文件中的 Python 语句
          >>> str1="print('hello world')"
          >>> exec(str1)
          hello world
            • compile(source, filename, mode[, flags[, dont_inherit]])
              • 将source编译为代码或者AST对象。代码对象能够通过exec语句来执行或者eval()进行求值。
                1、参数source:字符串或者AST(Abstract Syntax Trees)对象。
                2、参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。
                3、参数model:指定编译代码的种类。可以指定为 ‘exec’,’eval’,’single’。
                4、参数flag和dont_inherit:这两个参数暂不介绍
            str1 = "print('hello world')"
            c2 = compile(str1,'','exec')   
            exec(c2)
            
            str2="3+4"
            c3=compile(str2,'','eval')
            a=eval(c3)
            print(a)
                      • id(object)  :函数用于获取对象的内存地址
                      >>> id(str1)
                      1514678732384
                      >>> str2=str1
                      >>> id(str2)
                      1514678732384
                          • isinstance(object, classinfo):判断object是否是class的实例
                          >>> isinstance(1,int)
                          True
                          >>> isinstance(1.0,int)
                          False
                              • len(s)  :返回长度(ascll格式的返回字节数,unicode返回字符数/或元素个数)
                              >>> a=b'abc'
                              >>> len(a)
                              3
                              >>> b="我爱中国"
                              >>> len(b)
                              4
                              >>> c=[1,2,3,4]
                              >>> len(c)
                              4
                                  • repr(object)  :将对象转化为供解释器读取的形式,实质是返回一个对象的 string 格式
                                  >>> c=[1,2,3,4]
                                  >>> repr(c)
                                  '[1, 2, 3, 4]'
                                  >>> d={1:2,2:3,3:4}
                                  >>> repr(d)
                                  '{1: 2, 2: 3, 3: 4}'
                                      • type(object)  :返回该object的类型
                                      >>> type(1)
                                      <class 'int'>
                                      >>> type("123")
                                      <class 'str'>
                                      >>> type((1,2,3))
                                      <class 'tuple'>


                                      posted @ 2018-02-03 19:01  随风行云  阅读(280)  评论(0编辑  收藏  举报