Several Python function must know

Python comes with a few more interesting function, generally during the interview or written test basis to ask, which is 3 map, filter, reduce the function.

1.map(function, iterable)

It is the first element to pass the function name anonymous function or lambda expression, and the second element of the incoming iterables.

array = [1,2,3,4,5] result = map (lambda x: x + 1, array) result # result here is a result of direct printing object list need only click conversion 
can see the results of specific < map object at 0x0000000003A79518> list (result ) [2, 3, 4, 5, 6]

Here we can see, map function results in each element than the original list element corresponding to 1 large, and my lambda expression is the abbreviation of each element plus 1.

It can be seen that the map () function returns the object is to be in the iteration function of each element of the operation, and then after the element iterables operation consisting returned.

2.filter(function, iterable)

Similarly, the first argument passed to the function name or anonymous function, the second element of the incoming iterables

# Press function map incoming try = Array [1,2,3,4,5] Result = filter (the lambda X: X +. 1, Array) Result <Object filter AT 
 0x0000000003A79550> List (Result) 
# results No change of each parameter in the operation returns no [1, 2, 3, 4, 5] 
# the function True, False is determined to try to return result1 = filter (lambda x: x if x% 2 == 0 else None, array ) RESULT1 <Object filter AT 
 0x0000000003C28A90> List (RESULT1) 
# return a True value for the [2, 4]

Can be drawn from the above code, filter () function is iterable elements are substituted into the function, and then return to True elements, general screening done when you can use, do not write cycles.

3.reduce(function, iterable)

reduce function only in python2 the only incoming parameters and map, filter is the same. But its function is passed two elements, the first two elements substituting a value obtained after operating the function returns the value returned again as the first element, while the third element as the second element again substituting function operation, until the end of the return to the final result.

array = [1,2,3,4,5]
result = reduce(lambda x,y:x+y,array)
list(result)
#1+2+3+4+5=1515

It's actually very simple steps:

The first x = 1, y = 2 incoming anonymous function, x + y is a value returned 3

The second x = 3 (the return of 3), y = 3 (3 this second element in the list) into the function to return x + y is 6

Third x = 6, y = 4, x + y is 10 to return

Fourth x = 10, y = 5, x + y is 15 to return

No element can be passed directly back end.

Tips: python tool in the built-in function is not necessarily the fastest algorithm, just for convenience, write code fast, simple, if the pursuit of speed, then still have to blow your own hands optimal algorithm. (Interview topic so only so many algorithms and data structures)

This article is reproduced in https://www.py.cn/jishu/jichu/11175.html

Guess you like

Origin www.cnblogs.com/jsdd/p/11586208.html