A Python lambda expression

Source: Internet
Author: User



An anonymous expression---lambda

Describes an anonymous function-also a lambda expression.

It introduces several functions of sequence processing , such as sequence filtering, taking all the elements to do some operations, this kind of sequence processing function.

Such functions are characterized by the use of other functions to operate . We're going to use lambda here.

? Anonymous Functions

A lambda function is a minimal function that quickly defines a single line, borrowed from Lisp and can be used wherever a function is needed.

Lambda is an anonymous function, a function that does not require a name, it is more like a command, usually only need a line of code to do one thing, you can define it with Lambda .

You do not need a function name, only function function, it will be used.

Characteristics

◆ when using Python to write some execution scripts, using lambda eliminates the

The process of semantic functions, so that the code is more fine-barrel.

For some abstract functions that are not reused in places, sometimes

Waiting for a function name is also a problem, and using lambda does not need to be considered

Named question. Just once, the function that performs the function once, can choose a concise lambda

Using lambda makes your code easier to understand at some point.

Lambda Application Example
Reduce is a sequential list of each item, the received parameter is 2, and finally
Returned as a result
>>>defmyadd (x, y):
>>>
Returnx+y
>>>sum =reducc (Myadd, (+))
>>>6

Working principle

Reduc (with function operation, sequence (1,2,3,4,5))

Reduc used to transmit values

Myadd can be any function, This function must be able to accept two values

Reduce is passed to two values at a time ,

Demo reduce

Note in the py2, reduce is directly available, py3 the use of the hints are not defined,

The reason is that in the py3, reduce is placed in a function tool library where a module, (Func tools);

Tune function Tool Library code

From Functools import reduce


From the place to fetch what function, import reduce;

Chinese translation help (reduce)

Helps reduce _functools built-in function modules:

Reduce (... )

Decrease (function, sequence [initial value]) > value

The ability to use two parameters to accumulate items into a sequence,

From left to right, to reduce the sequence to a single value.

For example, decrease (Λx,y x,y,[1, 2, 3, 4, 5]) calculation

(((1 + 2) + 3) + 4) + 5). If it is initially present, it is placed before the project.

In the sequence of calculations, when

The sequence is empty.

Reduce Example

Run code

def c (x, y): Return X+yreduce (c,[1,2,3,4,5])

Express meaning

The definition of function C requires two values (x, y):

Returns the result of a two-value addition return X+y

Reduce requires a function to use

Reduce (c,[1,2,3,4,5,]) built-in function call function use

Now in this case, the function C defined inside is just for the reduce service.

It's only used once, so there's no need for code like this to appear in the program.

You can choose to use a lambda expression to define it

A lambda expression can have no name,

Directly followed by the parameters of the function,

There are several parameters to write X, Y: Back is the operation returned;

Returns an object that can be defined by a name

cc = lambda expression x,y:x+y

Usage: Lambda defines a value of two, giving it two values.

CC (2004,14)

The expression runs a total of two values,

So when you need to use this function like reduce, you don't have to define a function for it alone, you can simply write the lambda expression in.

Lambda An expression

Run code

Reduce (lambda x,y:x+y,[1,2,3,4,5])

Express meaning

Reduce direct (lambda x,y:x+y, [1,2,3,4,5] followed by a sequence, can get it in any way)

So in this case, there's no need to do this for me. This function can be used to define a function to be placed, which can be written directly with a lambda expression.

There are similar list-handling functions in addition to reduce,

Need to work with a function.

Sequence processing functions
filter ()

Fiter filtered, followed by a list of values, in accordance with the return
map

If you want to iterate over each element of the list, then use the Map,map to take the value in turn, get the value to define the operation, and return the result of each value operation.

Reducel

Reduce is a sequential list of each item, the received parameter is 2, and finally
Returned as a result

can be used in accordance with the conditions of use, lambda expression;

Although it looks neat, you can define a function without running a single thing. Note that this code, can read, if you read more laborious, this is not the result of Python, not all use lambda expression, can also use the list expression,

They all operate on a sequence;

List expression notation

lambda->List-expression
◆Example of Map, can be written as:
Print [x * 2 + Ten  for x inch foo]

print   ( [x   *   2   +   10   for   x   in   foo])

In Python3, the print statement is removed and the print () function is added to achieve the same functionality.


The filter example can be written as:
Print[x for x in foo if x%3 = = 0]

print ([x for x in foo if x%3 = = 0])

Py3 usage

So this kind of thing, according to your actual needs, to choose which one is more suitable, no more suitable

In most cases, use it according to your habits or preferences.

The first use efficiency is higher.

This chapter gets the knowledge points

1. Understanding the lambda expression syntax

2. And when to use

When applied to a function that is only executed once, a lambda can be defined and placed in a critical position.


2018-1.28 Day Wang Yulin



A Python lambda expression

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.