Detail Python Lambda function and sorting

  • 2020-05-12 02:52:33
  • OfStack

The lambda function is a quick one-line minimum function borrowed from Lisp and can be used wherever a function is needed. The following example compares the way traditional functions are defined with the lambda function.

The other day I saw 1 line of Python code for 1000 factorial

Python code


print  reduce(lambda  x,y:x*y,  range(1,  1001))  

1. I was impressed by the simplicity and compactness of python code, so I made a simple analysis of the code.

Both reduce and range are built-in functions of Python.

range (1, 1001) means generating a list of consecutive integers from 1 to 1000 (List).

reduce (functionA, iterableB), functionA are functions that require two variables and return one value. iterableB is an iterable variable, such as List, etc. The reduce function passes the elements of B into the function A from left to right, and then replaces the parameters with the results returned by the function A. If it is executed repeatedly, B reduce can be changed into a single value. In this case, a list of consecutive integers from 1 to 1000 is passed into the lambda function and the product of two Numbers is used to replace the Numbers in the list. The actual calculation process is :(...) (* 2) * (1 3) * 4) *... Times 1000), which is equal to 1000 factorial.

Now let's introduce the lambda function.

The lambda function is a quick one-line minimum function borrowed from Lisp and can be used wherever a function is needed. The following example compares the way traditional functions are defined with lambda functions:


>>> def f(x,y):  
...   return x*y  
...    
>>> f(2,3)  
 
>>> g = lambda x,y: x*y  
>>> g(2,3) 

As you can see, the result of the two functions is like 1. For functions with simple functions, the definition of lambda function is more concise and flexible. You can also directly assign the function to a variable and use the variable name to represent the function name.

In fact, the lambda function does not need to be assigned to a variable in many cases (such as the factorial process in the previous article).

Here are a few things to note about using the lambda function:

The lambda function can take any number of arguments, including optional arguments, and return the value of a single expression.

The lambda function cannot contain commands and cannot contain more than one expression.

Below is a simple demonstration of how to use the lambda function for custom sorting.


class People: 
  age=0 
  gender='male' 
 
  def __init__(self, age, gender):  
    self.age = age  
    self.gender = gender 
  def toString(self): 
    return 'Age:'+str(self.age)+'\tGender:'+self.gender 
 
List=[People(21,'male'),People(20,'famale'),People(34,'male'),People(19,'famale')] 
print 'Befor sort:' 
for p in List: 
  print p.toString() 
 
List.sort(lambda p1,p2:cmp(p1.age,p2.age)) 
print '\nAfter ascending sort:' 
for p in List: 
  print p.toString() 
 
List.sort(lambda p1,p2:-cmp(p1.age,p2.age)) 
print '\nAfter descending sort:' 
for p in List: 
  print p.toString() 

The code above defines an People class, and through the lambda function, the list of People class objects is sorted in ascending and descending order according to the age of People. The results are as follows:

Befor sort:
Age:21 Gender:male
Age:20 Gender:famale
Age:34 Gender:male
Age:19 Gender:famale

After ascending sort:
Age:19 Gender:famale
Age:20 Gender:famale
Age:21 Gender:male
Age:34 Gender:male

After descending sort:
Age:34 Gender:male
Age:21 Gender:male
Age:20 Gender:famale
Age:19 Gender:famale


Related articles: