Get the App
SLTechnology News&Howtos  ›  Development  › 

Explanation of map function and reduce function in python

Shulou Source: shulou.com Published: 2022-06-03 12:13:53 10月03日 Update

This article mainly explains "the knowledge points of map function and reduce function in python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "python map function and reduce function knowledge points to explain" it!

Map function and reduce function knowledge point explanation, according to the parameter, parameter map () contains two parameters, the first parameter is a function, the second is a sequence (list or tuple). The first argument of reduce () is the function, and the second is the sequence (list or tuple); depending on the numerical action, map () acts the passed function on each element of the sequence in turn, and reduce () acts the passed function on the first element of the sequence to get the result.

1. In terms of parameters:

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

The first argument to 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 effect of the transmitted values:

Map () acts the passed function on each element of the sequence in turn, and each element is "acted" once by the function alone.

Reduce () is to apply the passed-on function to the first element of the sequence and then continue to interact with the next element (cumulative calculation).

The map () function takes two arguments, one is a function, and the other is that Iterable,map acts the passed function on each element of the sequence in turn and returns the result as a new Iterator.

> def f (x):

... Return x * x

...

> r = map (f, [1,2,3,4,5,6,7,8,9])

> > list (r)

[1, 4, 9, 16, 25, 36, 49, 64, 81

The first argument passed in by map () is f, which is the function object itself. Since the result is an Iterator,Iterator, it is an inert sequence.

So through the list () function, let it calculate the entire sequence and return a list.

Reduce acts a function on a sequence [x1, x2, x3,...] This function must take two arguments

Reduce continues to accumulate the result with the next element of the sequence, and the effect is

Reduce (f, [x1, x2, x3, x4]) = f (f (x1, x2), x3), x4)

Age: write a str2float function using map and reduce to convert the string '123.456' into a floating point number 123.456

Def str2float (s):

Def fn (XBI y):

Return Xerox 10

N=s.index (.')

S1=list (map (int, [x for x in s [: n]]))

S2=list (map (int, [x for x in s [n + 1:]))

Return reduce (fn,s1) + reduce (fn,s2) / 10**len (S2)

Print ('\ '123.4567\' =', str2float ('123.4567'))

Python's built-in filter () function is used to filter sequences.

Like map (), filter () also receives a function and a sequence. Unlike map (), filter () acts the passed function on each element in turn

The element is then retained or discarded based on whether the return value is True or False. The filter () function returns an Iterator, which is a lazy sequence.

At this point, I believe that everyone on the "python map function and reduce function knowledge points to explain" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Function sequence parameter action element result two knowledge knowledge point transmission content inertia numerical value or learning different practical deeper that is position Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Linux Microsoft Xiaomi