zoukankan      html  css  js  c++  java
  • day13内置函数

    内置函数

    一、三元表达式

    1 def max2(x,y):
    2     if x>y:
    3         return x
    4     else:
    5         return y
    6 res=max2(10,11)
    7 print(res)

      三元表达式仅应用于:

       1、条件成立返回一个值

       2、条件不成立返回一个值

      res=x if x > y else y

      print(res)

    1 def max2(x,y):
    2     return x if x > y else y
    3 print(max2(10,11))

    二、函数递归

    定义:函数的递归调用,即在函数调用的过程中,又直接或间接地调用了函数本身

    1、直接调用

    1 def foo():
    2     print('from foo')
    3     foo()
    4 foo()

    2、间接调用

    1 def bar():
    2     print('from bar')
    3     foo()
    4 
    5 def foo():
    6     print('from foo')
    7     bar()
    8 foo()

    递归分为两个阶段

    1、回溯:

      注意:一定要在满足某种条件回溯,否则无限递归

    2、递推

    总结:

    1、递归一定要有一个明确的结束条件;

    2、每进入下一次递归,问题的规模都应该相应减少;

    3、在python中没有尾递归优化;

    1 def age(n):
    2     if n == 1:
    3         return 18
    4     return age(n-1)+2 #age(1)+2+2+2+2
    5 
    6 print(age(5))

    例:items=[1,[2,[3,[4,[5,[6,[7,[8,[9,[10,]]]]]]]]]],利用递归取出嵌套列表内10。

    1 items=[1,[2,[3,[4,[5,[6,[7,[8,[9,[10,]]]]]]]]]]
    2 def tell(l):
    3     for item in l:
    4         if type(item) is not list:
    5             print(item)
    6         else:
    7             tell(item)
    8 print(tell(items))

    三、匿名函数

    1 def foo(x,n): #foo=函数的内存地址
    2     return x ** n
    3 
    4 f=lambda x,n:x ** n
    5 print(f(2,3))

    强调:

      1、匿名的目的就是要没有名字,给匿名函数赋给一个名字是没有意义的;

      2、匿名函数的参数规则、作用域关系与有名函数是一样的;

      3、匿名函数的函数体通常应该是一个表达式,该表达式必须要有一个返回值;

    lambda x,y:x+y

    lambad匿名函数的应用:

    #max,min,sorted,map,filter

     1 info=[
     2     {'name':'egon','age':'18','salary':'3000'},
     3     {'name':'wxx','age':'28','salary':'1000'},
     4     {'name':'lxx','age':'38','salary':'2000'}
     5 ]
     6 # 计算出最大值
     7 res=max(info,key=lambda dic:int(dic['salary']))
     8 
     9 # 计算出最小值
    10 res=min(info,key=lambda dic:int(dic['salary']))
    11 
    12 # 进行排序
    13 res=sorted(info,key=lambda dic:int(dic['salary']))
    14 
    15 # 为真则生成新的列表
    16 res=map(lambda x:x**2,[1,2,3,4])
    17 
    18 # 找出大于2的数字
    19 res=filter(lambda x:x > 2,[1,2,3,4])
    20 
    21 
    22 
    23 print(list(res))

    四、内置函数

     1 print(abs(-1))
     2 print(all([1,'a',True])) # 列表中所有元素的布尔值为真,最终结果才为真
     3 print(all('')) # 传给all的可迭代对象如果为空,最终结果为真
     4 
     5 print(any([0,'',None,False])) #列表中所有元素的布尔值只要有一个为真,最终结果就为真
     6 print(any([])) # 传给any的可迭代对象如果为空,最终结果为假
     7 
     8 print(bin(11)) #十进制转二进制
     9 print(oct(11)) #十进制转八进制
    10 print(hex(11)) #十进制转十六进制
    11 
    12 print(bool(0)) #0,None,空的布尔值为假
    13 
    14 res='你好egon'.encode('utf-8') # unicode按照utf-8进行编码,得到的结果为bytes类型
    15 res=bytes('你好egon',encoding='utf-8') # 同上
    16 print(res)
    17 
    18 def func():
    19     pass
    20 print(callable('aaaa'.strip)) #判断某个对象是否是可以调用的,可调用指的是可以加括号执行某个功能
    21 
    22 print(chr(90)) #按照ascii码表将十进制数字转成字符
    23 print(ord('Z')) #按照ascii码表将字符转成十进制数字
    24 
    25 
    26 print(dir('abc')) # 查看某个对象下可以用通过点调用到哪些方法
    27 
    28 print(divmod(1311,25)) # 1311 25
    29 
    30 # 将字符内的表达式拿出运行一下,并拿到该表达式的执行结果
    31 res=eval('2*3')
    32 res=eval('[1,2,3,4]')
    33 res=eval('{"name":"egon","age":18}')
    34 print(res,type(res))
    35 # 
    36 with open('db.txt','r',encoding='utf-8') as f:
    37     s=f.read()
    38     dic=eval(s)
    39     print(dic,type(dic))
    40     print(dic['egon'])
    41 
    42 
    43 s={1,2,3}
    44 s.add(4)
    45 print(s)
    46 
    47 # 不可变集合
    48 fset=frozenset({1,2,3})
    49 
    50 x=111111111111111111111111111111111111111111111111111111111111111111111111111111111111
    51 print(globals()) # 查看全局作用域中的名字与值的绑定关系
    52 print(dir(globals()['__builtins__']))
    53 def func():
    54     x=1
    55     print(locals())
    56 func()
    57 print(globals())
    58 
    59 # 字典的key必须是不可变类型
    60 dic={[1,2,3]:'a'}
    61 # 不可hash的类型list,dict,set==  可变的类型
    62 # 可hash的类型int,float,str,tuple ==  不可变的类型
    63 hash()
    64 
    65 def func():
    66     """
    67     帮助信息
    68     :return:
    69     """
    70     pass
    71 
    72 print(help(max))
    73 
    74 
    75 len({'x':1,'y':2}) #{'x':1,'y':2}.__len__()
    76 
    77 obj=iter('egon') #'egon'.__iter__()
    78 print(next(obj)) #obj.__next__()
  • 相关阅读:
    Leetcode: Max Consecutive Ones II(unsolved locked problem)
    Leetcode: Find Permutation(Unsolve lock problem)
    G面经: Design Stock Price Display System
    U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column
    U面经Prepare: Web Address
    FG面经Prepare: BST to Double LinkedList
    G面经Prepare: Longest All One Substring
    G面经Prepare: Print Zigzag Matrix
    FG面经: Interval问题合集
    2017.10 有感而发(小结)
  • 原文地址:https://www.cnblogs.com/happyfei/p/9419837.html
Copyright © 2011-2022 走看看