Lambda functions in Python list comprehension and zip function usage Guide

Source: Internet
Author: User
Tags python list python list comprehension
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 lambda function can be written as: G = lambda x:x*2 ' g (3) The result is 6. (Lambda x:x*2) (3) ' is the same effect.

This is a lambda function that accomplishes 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 omitted (implied, because the entire function has only one row). Moreover, the function does not have a function name, but it can be assigned to a variable for invocation
You don't even need to assign a value to a variable when you use a lambda function. This may not be 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 parameters) and return the value of a single expression. A lambda function cannot contain commands and cannot contain more than one expression. Do not attempt to cram too much into the lambda function; If you need something more complicated, you should define a normal function, and then you want to make it long enough. I use them to encapsulate special, non-reusable code, and avoid flooding my code with a large number of single-line functions.

List derivation (listing comprehension)

See a simple code

Copy the 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 the Code code as follows:


>>> a = [+ +]
>>> 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 the 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 results of the C*m
Thought for a moment, and the last sentence changed to

Copy the Code code as follows:


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

Multiplication of two matrices

Learning Resources

Copy the 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)] ' for a in a]

A = [[up], [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.