Day 24: anonymous function

Anonymous function

Anonymous is no name, so there is no way to call him, and only in combination with certain methods

Anonymous function syntax: lambda parameter: Return value

But if you really want to call the anonymous function, it can be assigned, but he became a well-known function

f = lambda x,y : x**y
res = f(1,2)
print(res)

Basically anonymous functions and built-in method in combination with

max / min Back Max / Min

salary_dict = {
     'nick': 3000,
     'jason': 100000,
     'tank': 5000,
     'sean': 2000,
     'z': 1000
}

  def func(name):  # nick/jason/tank/sean/z
      return salary_dict[name]  # 3000/100000/5000/2000/1000
 res = max(salary_dict, key=func)  # 默认key的首字母

 res = max(salary_dict, key=lambda name: salary_dict[name])  # 默认key的首字母
 print(res)
 # key=func默认做的事情
 # 1. 循环遍历salary_dict,会取到所有的key值
 # 2. 然后把所有的key值依次丢入func中,返回薪资
 # 3. 通过返回的薪资排序,得到最大值

 res = min(salary_dict, key=lambda name: salary_dict[name])
 print(res)

fileter screening

def function(item):  # 1/2/3/4
    if item < 5:
         return True
     else:
         return False


res = filter(function, [1, 2, 3, 4])
res = filter(lambda item: item > 2, [1, 2, 3, 4])
print(res)  # 迭代器
print(list(res))

 salary_dict = {
     'nick': 3000,
     'jason': 100000,
     'tank': 5000,
     'sean': 2000,
     'z': 1000
 }

 res = filter(lambda item: salary_dict[item] > 3000, salary_dict)
 print(list(res))

map map

 def function1(item):
     return item + 2


 res = map(function1, [1, 2, 3, ])
 print(res)
 print(list(res))
    
   

salary_dict = {
     'nick': 3000,
     'jason': 100000,
     'tank': 5000,
     'sean': 2000,
     'z': 1000
 }

res = map(lambda salary:salary_dict[salary] +500,salary_dict)
print(list(res))

Built-in functions

Perhaps the use of

byte () decodes characters

char () / word ()

char () refer to ASCII code table corresponding to the number loaded into a string; the ord () converted into the corresponding number string Canon

divmod () divided by columns, programming remainder

print(divmod(10,3))  # (3,1)
#                 整数部   余数部

enumerate () English interpretation enumeration, python is with the iteration index

lt = ['a','b,'c']
for i in enumerate(lt)
    print(i) 
# (0,'a')
# (1,'b')
# (2,'c')

eval () evaluation, translated into the character string data type

lis = '[1,2,3]'
lis_eval = eval(lis)
print(lis_eval)
# [1,2,3]

If hash () can be determined hashing

Not necessarily used to

  1. abs: absolute value
  2. all: within the object can be iterative elements are all true, it returns true.
  3. any: iterables there is an element is true, True.
  4. bin / oct / hex: binary, octal, hexadecimal conversion
  5. dir: include all of the functional blocks.
  6. frozenset: immutable collection
  7. gloabals / locals: View global name / name to view the current location
  8. pow:
  9. round:
  10. slice:
  11. sum:
  12. __import__: import module string

The use of exception handling part

 异常处理: 报了错,进行处理
#
# print(1)
# num = input('请输入数字:')
#
# dic = {'a': 1}
#
# try:
#
#     print(dic['b'])  # KeyError
#     1 / int(num)  # 报错之后,不运行下面的代码
#
# except ZeroDivisionError:
#     print('傻逼,不能输入0')
# except KeyError:
#     print('傻逼,不知道什么错误')
#
# print(2)


# print(1)
# num = input('请输入数字:')
#
# dic = {'a': 1}
#
# try:
#
#     print(dic['b'])  # KeyError
#     1 / int(num)  # 报错之后,不运行下面的代码
#
# except Exception as e:  # 万能异常,只要有错误,就捕捉
#     print(e)  # 错误的描述信息
#     print('傻逼,不知道什么错误')
#
# print(2)

# # 异常捕捉只能捕捉逻辑错误
# fr = open('test.py')
# try:
#     # 文件中途报错
#     1 / 0
#     fr.close()
# except Exception as e:
#     print(e)
# finally:  # 无论你包不报错,都执行这一行
#     print('finally')



# assert 断言 + 条件--> 被历史淘汰了
#
# num = input('num:')
#
#
# assert 1==int(num)





# raise 主动抛错误

Guess you like

Origin www.cnblogs.com/lyyblog0715/p/11588561.html