# Map and reduce functions in Python

Time：2020-8-1

1. In terms of parameters:

Map() function:

Map () contains two parameters. The first is a parameter, which is a function, and the second is a sequence (list or tuple). The function (that is, the function at the first parameter position of the map) can receive one or more parameters.

Reduce() function:

The first parameter of reduce () is a function, and the second is a sequence (list or tuple). However, its function must take two parameters.

2. In terms of the numerical effect transferred in:

Map() is to apply the incoming function to each element of the sequence in turn, and each element is individually “acted” by the function once; (see chestnuts below)

Reduce() acts on the first element of the sequence with the passing function. After the result is obtained, the result is continued to interact with the next element (cumulative calculation),

The final result is the interaction of all the elements. (look at the chestnuts below)

Take a chestnut

Map() function:

``````#Pass in a parameter
def one_p(x):
return x * x
print 'map1.1:', map(one_p, range(1, 5))
#Results: map1.1: [1,4,9,16]
print 'map1.2:', map(one_p, [1, 2, 3, 4, 5, 6])
#Results: map1.2: [1,4,9,16,25,36]

#Pass in multiple parameters
a = [1, 2, 3, 4, 5]
b = [1, 1, 6, 2, 3]
c = [1, 2, 3, 4, 5]
s = map(lambda (x, y, z): x * y * z, zip(a, b, c))
print 'map2:', s
#Results: MAP2: [1,4,54,32,75]``````

The second example is 111，212，363，424，535 and then form a list

Reduce() function:

``````R1 = reduce (lambda x, Y: X * y, (2,2,6,2)) # operation process: (((2 * 2) * 6) * 2)
R2 = reduce (lambda x, Y: X * y, (2, 2, 6), 2) ා operation process: (((2 * 2) * 6) * 2)

Print 'R1:', R1 ා result: R1: 48
Print 'R2:', R2 ා result: R2: 48``````

## PHP uses redis to solve the problem of oversold

preface In the event of the second killing of goods, for example, the inventory of goods is only 100, but in the rush purchase activity, 200 people may rush to buy at the same time, so there is concurrency. If the order of 100 goods is completed and the inventory is 0, it may continue […]