directory

  • A. The map function
  • 2. Reduce function
  • 3. The filter function
  • Four. Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

The reduce/map/filter functions in Python are easily confused. Although functions are used to operate on iterators or elements in sequences, they are applicable to different scenarios.

A. The map function

The map function performs the same operation on each element in an iterable or sequence (e.g. +1 per element, etc.) and returns an iterator or list, as shown in the following example:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python reduce/map/filter function difference. Py @time :2021/05/18 07:37 @motto: A thousand miles without a small step, a river without a small stream, The wonderful program life needs to accumulate unremittingly! "" "def func1 (x) : # will print each element to calculate square values (" x x x x = % d = % d "% (x, x * x)) return * x x if __name__ = =" __main__ ": Print (list(value)) print("***"*20) print("***"*20) Value = map(lambda x:x*x, list1) print(list(value))  x=1 x*x=1 x=2 x*x=4 x=3 x*x=9 x=4 x*x=16 x=5 x*x=25 [1, 4, 9, 16, 25] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * [1, 4, 9, 16, 25]] "'Copy the code

It is worth noting that the map function returns an iterator. Note that the returned result can only be iterated once. If you need to use it more than once, please save the result and process it in advance.

Def func1 (x) : # # the values of each element to calculate square print (" x x x x = % d = % d "% (x, x * x)) return * x x if __name__ = =" __main__ ": List1 = [1,2,3,4,5] value = map(func1,list1) print(list(value) print(list(value)) [1, 4, 9, 16, 25] []Copy the code

It’s crazy, isn’t it? Why is the second output an empty list when there is nothing wrong with it? Since the map function returns an iterator that can only be iterated once, the solution is to simply return the iterator to a list when the result is obtained. Example:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python reduce/map/filter function difference. Py @time :2021/05/18 07:37 @motto: A thousand miles without a small step, a river without a small stream, The wonderful program life needs to accumulate unremittingly! "" "def func1 (x) : # # the values of each element to calculate square print (" x x x x = % d = % d "% (x, x * x)) return * x x if __name__ = =" __main__ ": List1 = [1,2,3,4,5] value = list(map(func1,list1)) print(list(value) print(list(value)) [1, 4, 9, 16, 25] [1, 4, 9, 16, 25]Copy the code

2. Reduce function

The reduce function is characterized by applying a two-parameter function accumulatively to the items of a sequence from left to right to merge the sequence into a single value (such as summing or multiplying list elements, etc.) and returning the final calculation result, which is a value, as shown in the following example:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python reduce/map/filter function difference. Py @time :2021/05/18 07:37 @motto: A thousand miles without a small step, a river without a small stream, The wonderful program life needs to accumulate unremittingly! From functools import reduce # def func1(x,y): # calculation results as the next to last time input print (" x = y = % d % d x * y = % d "% (x, y, x * y)) return x * y if __name__ = =" __main__ ": List1 = [1,2,3,4,5] Print (value) print(type(value)) print("***"*20) print("***"*20) # Print (value) print(type(value)) print(type(value)) ""  x=1 y=2 x*y=2 x=2 y=3 x*y=6 x=6 y=4 x*y=24 x=24 y=5 x*y=120 120 <class 'int'> ************************************************************ 120 <class 'int'> '''Copy the code

3. The filter function

** Filter function ** features: Filters elements in an iterable according to specific criteria (for example, filter all even numbers in a list, etc.), as shown in the following example:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python reduce/map/filter function difference. Py @time :2021/05/18 07:37 @motto: A thousand miles without a small step, a river without a small stream, The wonderful program life needs to accumulate unremittingly! "" "lis =,1,2,3,4,5,6 [0] # define screening even ordinary function def func4 (x) : Print (list(res5)) print(list(res5)) print(list(res5)) print(list(res5)) Print (func4,lis) print(list(res7)) print(list(res7)) print(list(res7)) print(list(res7)) print(list(res7)) [0, 2, 4, 6] [] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * [0, 2, 4, 6] [] "'Copy the code

Meng force? As it turns out, the filter function returns the same result as the map function, which can only be iterated once. The solution is the same as the map solution.

Four.Guess you like

  1. Python conditional derivations
  2. Python list derivations
  3. Python dictionary derivations
  4. Python variable argument *argc/**kargcs
  5. Python anonymous function lambda
  6. Python return logic determines expressions
  7. Python is differs from ==
  8. Python mutable and immutable data types
  9. Shallow and deep copies of Python
  10. Python exception Handling
  11. Python thread creation and parameter passing
  12. Python thread mutex Lock
  13. Python thread time Event
  14. The Python thread Condition variable Condition
  15. Python thread Timer Timer
  16. Python thread Semaphore
  17. Python thread Barrier object Barrier
  18. Python thread Queue Queue – FIFO
  19. Python thread queue LifoQueue – LIFO
  20. Python thread PriorityQueue PriorityQueue
  21. Python thread Pool ThreadPoolExecutor
  22. Python thread Pool ThreadPoolExecutor
  23. The Python Process module
  24. The Python Process Process is different from threading
  25. Python interprocess communication Queue/Pipe
  26. Python process Pool multiprocessing.pool
  27. Python GIL lock

Python reduce/map/filter functions

This article is published by the blog – Ape Say Programming Ape Say programming!