Python Lambda functions and sorting, and pythonlambda
Lambda functions are the smallest function that quickly defines a single row. They are borrowed from Lisp and can be used wherever functions are needed. The following example compares the definition of a traditional function with a lambda function.
A few days ago, I saw the Python code for a 1000 factorial line.
Python code
print reduce(lambda x,y:x*y, range(1, 1001))
The python code is simplified and compact, so the code is analyzed in a simple way.
Reduce and range are built-in functions of Python.
Range (1000) indicates generating a List of consecutive integers from 1 ).
Reduce (functionA, iterableB), functionA is a function that requires two variables and returns a value. IterableB is an iterative variable, such as List. The reduce function transmits the elements in B from left to right to function A, and then replaces the input parameters with the results returned by function A. After Repeated execution, B can be reduced to A single value. Here, it is to pass the continuous integer list from 1 to 1000 into the lambda function and replace the number in the list with the product of two numbers. The actual calculation process is :(... (1 × 2) × 3) × 4) ×... * 1000). The final result is the factorial of 1000.
The following describes the lambda functions.
Lambda functions are the smallest function that quickly defines a single row. They are borrowed from Lisp and can be used wherever functions are needed. The following example compares the definition of a traditional function with a lambda function:
>>> def f(x,y): ... return x*y ... >>> f(2,3) >>> g = lambda x,y: x*y >>> g(2,3)
As you can see, the results of the two functions are the same. For functions that implement simple functions, using lambda functions for definition is more streamlined and flexible. You can also assign a function to a variable directly, the variable name is used to represent the function name.
In fact, lambda functions often do not need to assign values to a variable (for example, the process of factorial in the previous article ).
Precautions for using lambda functions:
Lambda functions can receive any number of parameters (including optional parameters) and return the value of a single expression.
Lambda functions cannot contain commands and cannot contain more than one expression.
The following describes how to use the lambda function to implement 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 a People class and uses the lambda function to sort the list of People objects in ascending and descending order according to the age of People. The running result is 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
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.