Guide to use lambda list comprehension and zip functions in python

  • 2020-04-02 14:10:38
  • OfStack

Lambda functions

Python supports an interesting syntax that allows you to quickly define the smallest function in 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, instead of using a lambda function: g = lambda x: x*2 'g(3) = 6.(lambda x: x*2)(3)' also works.

This is a lambda function that does the same thing as the normal function above. Note the short syntax here: there are no parentheses around the argument list, and the return keyword is ignored (implicitly because the entire function has only one line). Also, the function does not have a function name, but it can be assigned to a variable for invocation
Using lambda functions you don't even need to assign it to a variable. This is probably not the most useful thing in the world, but it just shows that lambda is just an inline function.
In general, lambda functions can take any number of arguments, including optional arguments, and return the value of a single expression. Lambda functions cannot contain commands and cannot contain more than one expression. Don't try to cram too much into lambda; If you need something more complicated, you should define a normal function and make it as long as you want. I use them in cases where I need to encapsulate special, non-reusable code to avoid flooding my code with single-line functions.

List comprehension

Look at a simple piece of code


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


>>> 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
Put this to use
one


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)]

What it does: m is a matrix with the parameter c, and it computes the result of c*m
Think for a moment, the last sentence changed to


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

Two matrix multiplication

Learning resources


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 = [[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 articles: