Python higher order functions

Higher order function: simply speaking, one function can receive another function as a parameter. Such a function is called a higher order function

  • The map function will map the specified sequence according to the function provided, that is to say, it will act on the specified sequence

Syntax for

map(function,interable...)

 1 #Use map Function to double the values in the list
 2 numberList = [number for number in range(0,11)]
 3 def multiply(number):
 4         return number *2
 5 numberProcessList = list(map(multiply,numberList))
 6 print(numberProcessList)
 7 
 8 #Use map The combination of function and anonymous function doubles the value of list
 9 numberProcessList = list(map(lambda number:number *2,numberList))
10 print(numberProcessList)
  • Filter function is used to filter the elements of a sequence. It receives two parameters. The first is a function and the second is a sequence. Each element of the sequence is passed as a parameter to the function for judgment, and then returns True or False. Finally, the element that returns True is put in a new list

Syntax for

filter(function,interable)

 

1 #Use filter Function to filter the list odd
2 def isOdd(number):
3     return number % 2 == 1
4 numberProcess = list(filter(isOdd,numberList))
5 print(numberProcess)
6 
7 #Use filter Odd filtering of lists with anonymous functions
8 numberProcess = list(filter(lambda number:number % 2 == 1,numberList))
9 print(numberProcess)

 

  • reduce function is used to accumulate the elements of the specified sequence. First, function the first two elements of the sequence, then operate with the third element, and so on.

Syntax for

reduce(function,interable)

 1 from functools import reduce #call functools In Library reduce
 2 def add(numberOne,numberTwo):
 3     return numberOne + numberTwo
 4 def multiply(numberOne,numberTwo):
 5     return numberOne*numberTwo
 6 numberList = [number for number in range(1,6)]
 7 numberAdd = reduce(add,numberList)
 8 print(numberAdd)
 9 numberMul = reduce(multiply,numberList)
10 print(numberMul)
  • sorted function is used to sort the sequence

Syntax for

sorted(interable,key = function|reverse = False|True)

key is used to receive a comparison function and sort by this function

 1 #Use sorted Typical usage of function
 2 numbers = [3,4,6,1,-6,63,-45]
 3 numbersSorted = sorted(numbers)
 4 print(numbersSorted)
 5 numbersSorted = sorted(numbers,reverse = True)
 6 print(numbersSorted)
 7 
 8 #Use sorted Function to sort strings
 9 strings = ['cake','apple','Good','book','test','Air']
10 stringsSorted= sorted(strings)
11 print(stringsSorted)
12 stringsSorted= sorted(strings, reverse = True)
13 print(stringsSorted)
14 
15 #Use key Parametric sorted Function usage
16 #sorted()Function is also a higher-order function, which can also receive a key Function to implement custom sorting
17 numbers = [3,4,6,1,-6,63,-45]
18 numbersSorted = sorted(numbers,key=abs)
19 print(numbersSorted)
20 numbersSorted = sorted(numbers,key=abs,reverse=True)
21 print(numbersSorted)

Tags: Python Lambda

Posted on Tue, 03 Dec 2019 08:58:13 -0800 by DrDankWD