Python--lambda expression

A lambda expression is a kind of function, which can be said to be an anonymous function.

format:

lambda 参数1,参数2..参数n:表达式

example:

fun1 = lambda a, b: a * b
fun2 = lambda *args: args[0]
fun3 = lambda a: a + 1 if a % 2 == 0 else a - 1
fun4 = lambda x, y: print("这里:{}".format(x + y))
print(fun1(3, 4))
print(fun2(5, 4, 3))
print(fun3(5))
print(fun4(5, 9))
'''
输出:
12
5
4
这里:14
None
'''
  1. Observing the above several expressions, we can find that if the lambda expression is passed to a variable, then the variable becomes the function name. In fact, the variable gets the address of the function, so that the function can be called.
  2. It can be found that the value of the expression is the return value of the lambda expression. If the expression does not return a value, the return value is None.
  3. Lambda expressions can also achieve multiple parameter transfer and keyword parameter transfer, and expressions can also write ternary operations

effect:

Such a weird thing naturally has its own role, and the biggest role is to pass it as a parameter.
such as:

def fun(x: int, f):
    return f(x) * 4 + pow(f(x), 4)


print(fun(4, lambda x: x * x))

This can quickly pass a simple function as a parameter, without the need to write a very long function

Maybe you think this is not useful for birds, then the following ones may be more useful

map:

Map can map some data with certain rules. The usage is
map (rule, iterable data) to
take a list as an example:

lst = ['146', '4258', '23333']
mp = map(int, lst)
print(list(mp))
'''
输出:
[146, 4258, 23333]
'''

It can be found that map has done all the elements of the entire list according to the rule on the left (that is, to int).

At this point, you can think of it. The rule on the left can be a function, then the lambda expression can come in handy.

lst = [1256, 555, 999]
mp = map(lambda x: x - 700, lst)
print(list(mp))
'''
输出:
[556, -145, 299]
'''

If you change the previous operation, you need to traverse all the elements once, which is very troublesome

reduce

First of all, reduce is in functools, so
the format of the reduce function that needs to be imported is still:
reduce (rule, iterable data) rule function must have two parameters.
See the following example

import functools as ftl
lst = [5, 6, 8, 7, 9]
result = ftl.reduce(lambda x, y: x + y, lst)
print(result)
'''
输出:
35
'''

This example may be a bit weird. Intuitively, the elements of the list are directly summed.

In fact, it’s almost right, the reduce operation can be viewed like this, the first time:
x = 5, y = 6
return 5 + 6 the
second time:
x = 5 + 6, y = 8
return 5 + 6 + 8 the
third time :
X = 5 + 6 + 8, y = 7
return 5 + 6 + 7 + 8

is such a process, that is, the return result of the previous time is taken as the first parameter of the next time, and the second parameter is taken later.

sorted

Let's take a look at the role of sorted itself:

lst = [5, 6, 7, 5, 1]
lst2 = ['abc', 'cca', 'acd']
print(sorted(lst))
print(sorted(lst2))
'''
输出:
[1, 5, 5, 6, 7]
['abc', 'acd', 'cca']
'''

Yes, sorted can be sorted, numbers are from small to large by default, and characters are in dictionary order by default

But what if we sort for some special data?
For example: we want to sort the following list on the basis of k

lst = [{
    
    'k': 41, 'f': 11}, {
    
    'k': 14, 'f': 18}, {
    
    'k': 8, 'f': 77}]

At this time, another parameter of sorted is needed, namely the key parameter. As before, the key parameter receives the sorting rule. The rule here can be a specified type of value, such as specifying'k'

lst = [{
    
    'k': 41, 'f': 11}, {
    
    'k': 14, 'f': 18}, {
    
    'k': 8, 'f': 77}]
print(sorted(lst, key=lambda a: a['k']))
'''
输出:
[{'k': 8, 'f': 77}, {'k': 14, 'f': 18}, {'k': 41, 'f': 11}]
'''

It may not be clear here. The function in kay can receive a parameter. This parameter will pass each element in the list (in this case, a dictionary) and all you have to do is for each parameter you have to indicate what to base on put in order. What is pointed out here is that the value corresponding to'k' in the dictionary is sorted according to the sorting basis

Such sorting may still not meet the needs. For example, suppose I want to sort the remainder of 3 according to certain numbers, what should I do?
Then you need to use cmp_to_key in functools at this time.
If you have studied C++ and other sorting, you will know that there is a custom sorting. Py also supports the
use of the above functions to write rules and sort arbitrarily
(I will not talk about Py sorting here, after all, it is mainly about lambda expression formula)

import functools as ftl
lst = [5, 6, 3, 8]
print(sorted(lst, key=ftl.cmp_to_key(lambda x, y: -1 if x % 3 > y % 3 else 1)))

Guess you like

Origin blog.csdn.net/qq_36102055/article/details/107190513