A well-known function Dian
Before we given function are well-known function, which is based on the use of the function name.
def func():
print('from func')
func()
func()
func()
print(func)
from func from func from func <function func at 0x10518b268>
Two Dian anonymous function
Anonymous function, he has no name binding, ie, once recovered, either bracketed run.
lambda x, y: x+y
<function __main__.<lambda>(x, y)>
res = (lambda x, y: x+y)(1, 2)
print(res)
3
Wed and built-in functions associated with
Generally anonymous functions max (), sorted (), filter (), sorted () method in combination.
salary_dict = {
'fujiachen': 3000,
'nash': 100000,
'jinyi': 5000,
'langyigang': 2000
}
1. If we want to remove the highest-paid people from the dictionary, we can use the max () method, but the max () the comparison is the default dictionary key.
1. First iterables into iterator object
2.res = next (iterator object), the parameters passed to the function key res as specified, and returns the value as determined according to the function
salary_dict = {
'fujiachen': 3000,
'nash': 100000,
'jinyi': 5000,
'langyigang': 2000
}
print(f"max(salary_dict): {max(salary_dict)}")
def func(k):
return salary_dict[k]
print(f"max(salary_dict, key=func()): {max(salary_dict, key=func)}")
# 'fujiachen', v1 = func('fujiachen')
# 'nash', v2 = func('nash')
# 'jinyi', v3 = func('jinyi')
# 'langyigang', v4 = func('langyigang')
print(
f"max(salary_dict, key=lambda name: salary_dict[name]): {max(salary_dict, key=lambda name: salary_dict[name])}")
max(salary_dict): nash max(salary_dict, key=func()): nash max(salary_dict, key=lambda name: salary_dict[name]): nash
2. If we want the dictionary in the above-mentioned people descending order according to salary, you can use the sorted () method.
sorted () works:
1. First iterables into iterator object
2.res = next (iterator object), res as the argument to the function specified by the first argument, then the return value of the function as a judgment basis.
lis = [1, 3, 2, 5, 8, 6]
sorted(lis)
print(f"lis: {lis}")
print(f"sorted(lis,reverse=True): {sorted(lis,reverse=True)}")
lis: [1, 3, 2, 5, 8, 6] sorted(lis,reverse=True): [8, 6, 5, 3, 2, 1]
salary_dict = {
'fujiachen': 3000,
'nash': 100000,
'jinyi': 5000,
'langyigang': 2000
}
print(
f"sorted(salary_dict, key=lambda name: salary_dict[name]): {sorted(salary_dict, key=lambda name: salary_dict[name])}")
sorted(salary_dict, key=lambda name: salary_dict[name]): ['langyigang', 'fujiachen', 'jinyi', 'nash']
3. If we want a list of names to do a deal, you can use the map () method.
map () works:
1. First iterables into iterator object
2.res = next (iterator object), res as the argument to a function specified by the first parameter, and returns the value as the map () method of the function one of the results.
name_list = ['fujianchen', 'langyigang', 'jinyi']
res = map(lambda name: f"{name} sb", name_list)
print(f"list(res): {list(res)}")
list(res): ['fujianchen sb', 'langyigang sb', 'jinyi sb']
4. If we want to filter contains the name of the name 'sb' in addition, we can use the filter () method.
filter () works:
1. First iterables into iterator object
2.res = next (iterator object), res as the argument to a function specified by the first parameter, then the value returned will determine the filter function of the true and false, if is true then left.
name_list = ['nash', 'langyigang sb', 'fujianchen sb', 'jinyi sb']
filter_res = filter(lambda name: name.endswith('sb'), name_list)
print(f"list(filter_res): {list(filter_res)}")
list(filter_res): ['langyigang sb', 'fujianchen sb', 'jinyi sb']