Python high-order function usage summary!

**Author: Super Paper money, Xiamen University, Datawhale original author **

This article is about 2000 words, it is recommended to read it for 6 minutes**

Reviewer: Yuanhao Geng, member of Datawhale, East China Normal University, core contributor to the open source tutorial "Joyful-Pandas".

This article explains in detail the use of the 5 built-in higher-order functions of Python combined with various practical examples, which can help understand the data structure of Python and improve the efficiency of data processing. The 5 functions are:

One, map

1.1 Syntax

The basic syntax of map function is map(func, seq), and its meaning means: execute the function of the previous function func for each element in the iterable sequence, and finally obtain a new sequence. note:

help(map)  #View help information

1.2 demo

Illustrate how to use the map function with examples

  1. Use Python built-in functions

  1. Use custom functions

  1. Use anonymous function lambda

There can be multiple parameters when using anonymous functions

Two, reduce

2.1 Syntax

Definition of reduce function:

reduce(function, sequence [, initial])-> value

reduce takes an element from sequence in turn, and uses the result of the last call to function as a parameter, and calls function again.

"When calling a function for the first time, if the initial parameter is provided, the function will be called with the first element in the sequence and initial as the parameters, otherwise it will be called with the first number of the sequence

**2.2 Use **

In Python3, the reduce function has been moved to the functools module, and it needs to be imported first:

from functools import reduce   #Import

help(reduce)  #View help documentation

In the above example, we use a graphic to explain:

2.3 demo

  1. Use custom functions

  1. Use anonymous function lambda

image-20201024185550970

  1. A complex example

The specific process is:

1.1*2+1=32.3*3+1=10 # The first 3 is the result 3 above, and the second is 33 in the original data.10*4+1=41
  1. Example with initial value

The initial value and the first value in the sequence execute the func function, and the result will be the next starting value

# The specific process is explained as

1.6+1=72.7+2=93.9+3=124.12+4=165.16+5=21

Three, filter

3.1 Syntax

The filter() function is used to filter the sequence, filter out those elements that do not meet the conditions, and return the elements that meet the conditions to form a new list.

Each element in the sequence is passed as a parameter to the function for judgment, returns True or False, and finally the element that returns True is placed in the new list.

filter() syntax is as follows:

filter(function, iterable)  #The former is a function, the latter is a sequence to be executed

3.2 demo

help(filter)  #Help document

  1. Use custom functions

Return even numbers within 10

  1. Use anonymous function lambda

  1. Filtering of strings

Select a string that meets the specified requirements

Four, sorted

4.1 Syntax

sorted(iterable, key=None, reverse=False)

4.2 3 Parameters

sorted() accepts 3 parameters, returns a sorted list

4.3 demo

help(sorted)  #Help document

  1. Not inverted by default

  1. Sorting tuples, range objects, dictionaries

4.4 Result reversed

The meaning of result reversal is sort the results in descending order, because the original default is ascending order, and reverse=True is used.

4.5 Understanding key

The function of the key parameter is that we customize a function, and then sort by applying the elements in the sequence to the function

Here we use absolute value function

4.6 Contrast sort()

The sort() method can only sort the original list, and the parameters are the same as sorted

The result is that the original list is directly modified in place, while sorted is to generate a new list, the two are different

Five, zip

zip() is a very important method in Python, which can quickly implement many functions.

5.1 Syntax

zip([iterable,...])  #iterable is one or more iterators

help(zip)  #View document

5.2 zip accepts a sequence

zip can accept lists, tuples, strings and other forms

zip accepts the form of an empty list, and the returned list is still empty

5.3 zip accepts multiple sequences

Simultaneously merge different types of sequences

5.4 Different processing length

When multiple sequences exist at the same time, take the length of the smallest sequence

*5.5 zip(iterables)

We generally think that this method is the inverse process of zip, it is a process of unzip, for example to illustrate its use:

5.6 Complex example

Let’s look at a more complex example

The explanation of this example is:

  1. [ x] is a list containing lists, x itself is a list
  2. [ x]*3 The result is [x,x,x], which is actually [[4,5,6],[4,5,6],[4,5,6]]
  3. [*[ x]*3] The result is [(4,4,4),(5,5,5),(6,6,6)]

5.7 zip usage

The following is a practical example of zip to illustrate its application:

  1. List sum

  1. Data consolidation

  1. Dictionary key-value conversion

For loop implementation:

Use zip to achieve:

Recommended Posts

Python high-order function usage summary!
Python high-order function usage summary!
Python advanced usage summary
Python decorator simple usage example summary
Python basic summary
Python enumerate() function
Python function buffer
Python3 built-in module usage
Python custom function basics
Python processing json summary
Join function in Python
Python built-in function -compile()
Python function basic learning
Python interview questions summary
Python data analysis-apply function
Python3 built-in function table.md
Python Print print timer function
MongoDB usage in Python
Summary of ubuntu usage
Python defines a function method
Python realizes online translation function
Python datetime processing time summary
Python tornado upload file function
Python magic function eval () learning
How Python implements FTP function
LeetCode brushing questions summary python3
Python implements image stitching function
Python telnet login function implementation code
The usage of wheel in python
Python| function using recursion to solve
Summary of logarithm method in Python
Analysis of usage examples of Python yield
How Python implements the mail function
Why doesn't Python support function overloading?
Is there function overloading in python
Learning Python third day one-line function
Python novice learns to raise usage
Python list comprehension operation example summary
Python function definition and parameter explanation
Python ATM function implementation code example
Detailed usage of dictionary in Python
Usage of os package in python
A summary of 200 Python standard libraries!
Python implements AI face change function
Python realizes image recognition car function
Simple usage of python definition class
Summary of Python calling private attributes
Python implements ftp file transfer function
Python realizes udp transmission picture function
How Python implements the timer function
The usage of tuples in python
Python learning os module and usage