Guidelines for using Lambda functions list comprehension and zip functions in Python _python

Source: Internet
Author: User
Tags in python

Lambda functions

Python supports an interesting syntax that allows you to quickly define the smallest function of a single line. These functions, called Lambda, are borrowed from Lisp and can be used wherever a function is needed.

def f (x): Return x*2, using a lambda function to replace what can be written as: G = lambda x:x*2 ' g (3) results are 6. (Lambda x:x*2) (3) ' is the same effect.

This is a lambda function that completes the same thing as the normal function above. Note the short syntax here: there is no parentheses around the argument list, and the return keyword is ignored (implied because there is only one row for the entire function). Also, the function does not have a function name, but it can be assigned to a variable to call
When you use a lambda function, you do not even need to assign it to a variable. This is probably not the most useful thing in the world, it just shows that the lambda function is just an inline function.
In general, a lambda function can receive any number of arguments, including optional arguments, and returns the value of a single expression. A lambda function cannot contain a command and cannot contain more than one expression. Don't try to cram too much into a lambda function; If you need something more complex, you should define a normal function and how long you want it to be. I use them on the need to encapsulate special, reusable code to avoid flooding my code with a lot of single-line functions.

List derivation (comprehension)

Read a simple piece of code

Copy Code code as follows:

Testlist = [1,2,3,4]
def mul2 (x):
Print x*2
[Mul2 (i) for I in Testlist]
[Mul2 (i) for i in Testlist if i%2==0]

Multidimensional array Initialization
multilist = [[0 for Col in range (5)] for row in range (3)]

Zip function

Copy Code code as follows:

>>> a = [1,2,3]
>>> B = [4,5,6]
>>> C = [4,5,6,7,8]
>>> zipped = Zip (a,b)
[(1, 4), (2, 5), (3, 6)]
>>> Zip (a,c)
[(1, 4), (2, 5), (3, 6)]
>>> Zip (*zipped)
[(1, 2, 3), (4, 5, 6)]

Learning Resources
Apply
One

Copy Code code as follows:

m = [[ -1.0, 2.0/c-1, -2.0/c+1, 1.0],
[2.0, -3.0/c+1, 3.0/c-2,-1.0],
[-1.0, 0.0, 1.0, 0.0],
[0.0, 1.0/c, 0.0, 0.0]]
multiply = Lambda x:x*c
m = [[Multiply (M[col][row]) for Col in Range (4)] for row in range (4)]
print [[M[col][row] for Col in range (4)] for row in range (4)]

The work it does: M is a matrix that contains the parameter C, and he calculates the result of the c*m.
Think for a moment, the last sentence to change

Copy Code code as follows:

print [[Multiply (each) for each in row] for row in m] more pythonic

Multiplication of two matrices

Learning Resources

Copy Code code as follows:

def matrixmul (A, B):
res = [[0] * len (b[0]) for I in Range (Len (a))] for I in range (Len (a)):
For j in Range (Len (b[0)):
for k in range (len (B)):
RES[I][J] + = a[i][k] * B[k][j] return res
def matrixMul2 (A, B):
return [[Sum (A * b for a, b in zip (A, b)) for B in Zip (*b)]

A = [[1,2], [3,4], [5,6], [7,8]]
b = [[1,2,3,4], [5,6,7,8]]
Print Matrixmul (a,b) print Matrixmul (b,a) print "-" *90
Print MatrixMul2 (a,b) print matrixMul2 (b,a) print "-" *90

Related Article

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.