Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky

Python’s built-in higher-order functions

map()

Map () maps the specified sequence based on the provided function

Syntax format

map(function, iterable, ...)
Copy the code

The first argument function calls function with each element in the argument sequence,

The second argument iterable one or more sequences

Returns a new list containing the values returned by each function.

The sample code

List1 = [1, 2, 4, 5, 56, 12, 5, 2, 34] list1 = [1, 2, 4, 5, 56, 12, 5, 2, 34] Return lt + 1 list2 = map(func, list1) # do not add () # use lambda keyword list3 = map(lambda I: i if i % 2 == 0 else i + 1, list1) print(list(list3)) # [2, 2, 4, 6, 56, 12, 6, 2, 34] print(list(list2)) # [2, 2, 4, 6, 56, 12, 6, 2, 34]Copy the code

To reduce () function

Reduce () was a built-in function when Python2x was used, but by Python3x it was included in the FuncTools library

The reduce() function accumulates the elements in the argument sequence.

Function performs the following operations on all the data in a data set (linked list, tuple, etc.) : operates on the first and second elements of the set with function (which has two parameters) passed to Reduce, and then calculates the result with the third data using function function, finally obtaining a result.

Syntax format

reduce(function, iterable[, initializer])
Copy the code

Function — A function that takes two arguments

Iterable — iterable

Initializer — Optional, initial parameter

Returns the result of the function evaluation.

The sample code

from functools import reduce

list1 = [1, 2, 3, 4, 5, 6, 7]
value = reduce(lambda x, y: x + y, list1)
print(value)  # 28 = 1+2+3+4+5+6+7
Copy the code

It’s going to store the results in X, and it’s going to accumulate each time. Initializer is the initial value for setting x

The filter () function

The filter() function is used to filter a sequence, filter out elements that do not meet the criteria, and return an iterator object that can be converted to a list using list().

This takes two arguments, the first a function and the second a sequence. Each element of the sequence is passed as an argument to the function for evaluation, and then returns True or False. Finally, the element that returns True is placed in the new list.

Grammatical structure

filter(function, iterable)
Copy the code

Function — Judge the function.

Iterable — iterable.

Returns an iterable

Sorted () function

The sorted() function sorts all iterable objects and returns a new list.

Grammatical structure

sorted(iterable, cmp=None, key=None, reverse=False)
Copy the code

Iterable — iterable.

CMP — the comparison function, which takes two arguments, both of which are taken from the iterable, and which must obey the rules that return 1 if greater than, -1 if less than, and 0 if equal.

Key — A comparison element that takes only one argument. The argument to the function is taken from the iterable, and an element in the iterable is specified for sorting.

Reverse — Collation, reverse = True descending, reverse = False ascending (default).

Returns a reordered list.

The sample code

students = [ {'name': 'tom', 'age': 20}, {'name': 'lucy', 'age': 15}, {'name': 'lily', 'age': 13}, {'name': 'mark', 'age': 21}, {'name': 'jack', 'age': 13}, {'name': 'steven', 'age': Result = filter(lambda x: x['age'] > 18, students) print(list(result)) # [{'name': 'Tom ', 'age': 5}, {'name': 'mark', 'age': 21}] 5} X [' age '], reverse = True) # using key print (students) "' [{' name ':' mark ', 'age: 21}, {' name' : 'Tom', 'age: 20}, {" name" : 'steven', 'age': 18}, {'name': 'lucy', 'age': 15}, {'name': 'lily', 'age': 13}, {'name': 'jack', 'age': 13}] '''Copy the code