Learn Lambda and Make Your Program Pythonic

Lambda is a high-level usage in Python. To write code in Pythonic, you need to understand these high-level usage. If you want to say that you are a real Python programmer, you must first write the code in Pythonic.

Let ’s talk about Lambda usage today and write a short usage note.

Lambda means anonymous function. In the program, we may only use some simple functions once or twice. In this case, you don’t need to write it as a function. Instead, you use Lambda to make the program more concise.

Lambda’s writing prototype is:
lambda Parameter: operation (parameter)

The left side of the colon is the parameter passed in, and the right side of the colon is the operation to use the parameter.

such as

lambda x:x+1

The above is equivalent to the following, add 1 to the parameter and return:

def plus(x):
return x+1
such as
lambda x,y:x*y

The above is equivalent to:

def mul(x,y):
retuen x*y

Give a few practical examples:
1. For example, to sort the list, the following simple list
nums = [3, 1, 7, 4, 8, 10]
sort nums in descending order, this is very simple, right:

>>>nums = [3, 1, 7, 4, 8, 10]>>>sorted(nums, reverse=True)>>>[10, 8, 7, 4, 3, 1]

What about sorting a nested list?
For example: nums = [[10, 20, 11], [3, 9, 6], [8, 14, 3]]
We want to sort in descending order by the second value in each element

You can’t sort directly using the sorted wording above. At this time, it is very convenient to use lambda:

>>>nums = [[10, 20, 11], [3, 9, 6], [8, 14, 3]]
>>>sorted(nums, key=lambda x:x[1], reverse=True)
>>>[[10, 20, 11], [8, 14, 3], [3, 9, 6]]

The nested list is sorted in descending order according to the second value of the element. The lambda means to return the second value of the element. It is written as follows:

>>>def lis(x):
>>> return x[1]
>>>sorted(nums, key=lis, reverse=True)
>>>[[10, 20, 11], [8, 14, 3], [3, 9, 6]]

By comparison, writing code with lambda is a lot more introduction.

2. Another example is to sort in descending order by dictionary value

nums = {'yuan':80, 'ren':93, 'xue':60}
nums = {'yuan':80, 'ren':93, 'xue':60}
sorted(nums, key=lambda x: nums[x], reverse=True)

3. For example, if you want to take the maximum value in the list
nums = [‘1’, ‘100’, ‘111’, ‘2’, 2, 2.57]
but there are strings in the list, you cannot directly compare the size, how to do it, Use lambda for processing.

>>>nums = ['1', '100', '111', '2', 2, 2.57]
>>>max(nums, key=lambda x:int(x))
>>>'111'

This writing is equivalent to

def trsn(x):
return int(x)
max(nums, key=trsn)

If lambda is combined with higher-level Python syntax such as map and filter, there are more uses.

The advantage of using a lambda as above is to make the code more concise, which is a programmer’s pursuit of code. The disadvantage is that the program is not easy to understand, but as long as you understand the usage of lambda, it is not so difficult to understand.

#python #lambda #pythonic

Learn Lambda and Make Your Program Pythonic
1 Likes20.90 GEEK