Python has a lot of built-in functions, and it helps to be flexible. This article currently includes: Slice () set () round () map () Zip () filter () reduce () (not built, belonging to the Functools Library) sorted ()
It is worth noting that after Python3 , the map (), zip (), filter () returns the object is no longer a list , but an iterator .iterators are very important in Python , and in doubt you can see:Analysis of the use of Python yieldIterabl
There are two ways to define a function in Python, one defined by the normal definition of Def, a function to specify a name, and the second to be defined with a lambda, without specifying a name, called a lambda function.Lambda function is also called anonymous function, anonymous function is no Name function, function does not have a name? Yes, of course. Some functio
, DateTime>(Datestrings,Delegate(strings) {returnDateTime.ParseExact (s),"YYYY-MM-DD",NULL);});//with the addition of language features such as lambda expressions and extension methods, as well as the automatic judgment of the paradigm type, the use of anonymous methods in C # 3.0 is more concise and even the same as Ruby's syntaxIEnumerableDateTime>dates=Datestrings.select (S=DateTime.ParseExact (s),"YYYY-MM-DD",NULL));ReferenceHttp://blog.csdn.net/s
be a type error.2.reduce ()Format:reduce( func, seq[, init] )The reduce function is a simplification, which is a process in which the last iteration result (the element with the first init, such as the first element of the SEQ without init), executes a two-dollar func function with the next element, each iteration. In the reduce function, init is optional and, if used, is used as the first element of the first iteration.In simple terms, you can use a visual formula to illustrate:reduce( func, [
Lambda functions
Python supports an interesting syntax that allows you to quickly define the minimum function for a single line. These functions, called Lambda, are borrowed from Lisp and can be used wherever functions are needed.
def f (x): Return x*2, replaced with a
Python uses lambda functionsLearning Resources
Lambda function Learning
List comprehension
Multidimensional array Initialization
Lambda functions
Python supports an interesting syntax that allows you to quickly define the minimum function for a single line. These
the last iteration result (the element with the first init, such as the first element of the SEQ without init), executes a two-dollar func function with the next element, each iteration. In the reduce function, init is optional and, if used, is used as the first element of the first iteration.In simple terms, you can use a visual formula to illustrate:reduce( func, [1, 2,3] ) = func( func(1, 2), 3)The following is a diagram of the work process of the reduce function:For example, factorial is a
Lambda and Pythonlambda functions in Python
When I was reading a book today, I saw such a statement:
if isinstance(value,int) or isinstance(value,float): split_function=lambda row:row[column]>=value
Lambda is not clear, so I read the Python documentation, which explains the following:
, the rest should also be written to bubblsort (items, (First, second) =>{ comparecount ++; return first
Note that the left side of the lambda expression must be written () even if no parameter exists ()
The statement just now contains braces and is a Lambda statement. The expression Lambda has only one expression, which is written as follows:
// Expression
Python: high-order functions and lambda expressions, pythonlambda
Reason:
The python syntax is simple at a glance, but it is used in NLP. I think it is a bit easy to understand because it is less practical.
Read the source code of youtube_dl and see the usage of the filter () function and the lambda expression in it. If it is interesting, complete the lesson and
A few days ago, I saw the Python code for a 1000 factorial line.
Python code
printreduce(lambdax,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.
Range1, 1001) indicates generating a List of consecutive integers from 1 to 1000 ).
ReducefunctionA, iterableB), functionA is a function that requires two variables and returns a value. IterableB
Filter (function or None, sequence), where sequence can be list, tuple,string. The function of this function is to filter out all the elements in the sequence.
Filter (function or None, sequence), where sequence can be list, tuple,string. The function of this function is to filter out all elements in the sequence that return TRUE or bool (return value) to true when invoking the function as the element itself, and return with the list. Filter can only accept two parameters (Function,sequence), w
A delegate is a type of reference method. Once a method is assigned to the Delegate, the delegate has the same behavior as the delegate. A call to a delegate method can have parameters and return values like any other method.
Delegation has the following features:
Delegation is similar to C ++ function pointers, but they are type-safe.
The delegate allows passing methods as parameters.
A delegate can be used to define a callback method.
The delegate can be linked together. For
Python basic Tutorial: anonymous functions lambda and pythonlambda
Python lambda
When using a function, sometimes you do not need to define a function that is displayed. You can use an anonymous function to make it more convenient and support anonymous functions in Python.
For example, if we want to calculate two numbe
Filter (function or None, sequence), where sequence can be list, tuple,string. The function is to filter out all the sequence in the element itself ...
Filter (function or None, sequence), where sequence can be list, tuple,string. The function is to filter out all elements in sequence that return TRUE or bool (return value) to true when calling function as parameters, and return as a list. Filter can only accept two parameters (Function,sequence), where only one value can be returned in functio
Today, while reading a book, I saw a statement like this:if or isinstance (value,float): split_function=LambdaThe lambda function is very confused, so I looked at the Python document, which is explained in the following document:
Lambda
An
anonymous-inline function consisting of a single
expression which was evaluated when the function is Called. The syntax to create a
difference between the two>>> help (List.sort)-on Method_descriptor:sort (...) L.sort (Key=none, Reverse=false), None--stable sort *in place*>>> help (sorted) to built-in/, *, Key=none, reverse=False) from inch ascending order. and the in descending order.2. In addition to using operator, we can also use lambda>>> L.sort (key=Lambda x:x[1])>>> l[('C', 1 ), ('b', 2), ('a', 3)]3. Use sorted
1. Lambda functionLambda () is an anonymous function in Python with the following syntax:Lambda [arg1[, Arg2, ... ArgN]: expressionWhen learning conditional operations, for simple if else statements, you can use the ternary operation to indicate that:# Common conditional Statements if 1 = =1 :'evescn'else :' GM ' # Ternary Operations ' EVESCN ' if Else ' GM 'For simple functions, there is also an eas
, 64,125,216,343,512,729,100 0] >>> def cube (x): Return x + X... >>> map (cube, "ABCDE") ['A', 'bb ', 'cc', 'dd', 'ee']
In addition, MAP also supports multiple sequence, which requires that the function also supports the corresponding number of parameter inputs:
>>> Def add (x, y): Return x + y >>> map (ADD, range (8), range (8) [0, 2, 4, 6, 8, 10, 12, 14] 4. Reduce ()
Reduce (function, sequence, starting_value): calls the function sequentially for items in sequence. If starting_value exist
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.