python There are three functional programming greatly simplifies the complexity of the program , Let's make a discussion and record here .
One Map： Function applied to input all elements in the linked list , Its format is as follows ：
Most of the time , We will input the elements of a linked list into the function one by one to get the result , The code is as follows ：
items = [1, 2, 3, 4, 5]
squared = 
for i in items:
map You can simplify this function , As shown below ：
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))
Most of the time , We put lambda combination map Use it together , Replace traditional input , As shown below ：
return (x+x) funcs = [multiply, add]
for i in range(5):
value = list(map(lambda x: x(i), funcs))
print(value) # Output:
# [0, 0]
# [1, 2]
# [4, 4]
# [9, 6]
# [16, 8]
Two Filter： Create a function that returns to True Linked list elements of , Here's a simple example ：
number_list = range(-5,5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero) number_list = range(-5,5)
less_than_zero = list(filter(lambda x: x != 0, number_list))
3、 ... and reduce： It is widely used in the loop calculation of linked list elements ,
Under normal circumstances , The calculation is written like this ：
product = 1
list = [1, 2, 3, 4]
for num in list:
product = product * num # product = 24
In the use of reduce Under the circumstances , This is how it is calculated ：
from functools import reduce
product = reduce((lambda x, y: x * y), [1, 2, 3, 4]) # Output: 24
It's a lot simpler all at once .
Reference documents ：
- [ translate ]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE
map, filter, and reduce Python Several functions are provided , Enables functional programming . These functions all have convenient features , They can use it very easily python To write . Functional programming is all about expressions . We ...
- Python Built in functions Map,Filter and Reduce
Python Advanced map,filter, reduce yes python frequently-used built-in function. And often with lambda Expressions are used together . among : map form :map(function_to_ ...
- Python Map, Filter and Reduce
Website classification : python Basics > function author : Huiya Link to the original text : http://www.pythonheidong.com/blog/article/21/ source :python Black hole network www. ...
- Map,Filter and Reduce
from :https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG Abstract :Map,Filter and Reduce Three functions ...
- Map, filter and reduce
To add up all the numbers in a list, you can use a loop like this: Total is initialized to 0. Each t ...
- python Of map Functions and reduce function （ turn ）
map function map() function map() yes Python Built in higher-order functions , It receives a function f And a list, And by putting the function f Acting in turn list On each element of , Get a new one list And back to . example ...
- js Array Medium map, filter and reduce
Part of the source code in the original text comes from :JS Array.reduce Realization Array.map and Array.filter Array Higher order functions in ---- map, filter, reduce map() - ...
- python The higher-order function of (map,filter,sorted,reduce)
Higher order function Official account " Easy to learn programming " Learn more about . 1.MapReduce MapReduce It is mainly used in distributed system . Big data is actually 15 The fire started in the second half of the year . Distributed thinking : Put a continuous word ...
- [Python Learning notes -002] lambda, map, filter and reduce
1. lambda lambda, Anonymous functions , It can be understood as following C Language macros are similar to . for example : >>> max = lambda x, y: x if x > y else y >& ...
- Fullscreen API Full screen web page
- Unity The road of mobile travel < 11、 ... and > Resource Package Assetbundle
http://blog.csdn.net/janeky/article/details/17652021 In the operation of mobile games , Updating resources is indispensable . The first step of resource management is resource packaging . Traditional packaging can make all things into ...
- RDIFramework.NET ━ 9.3 User management ━ Web part
RDIFramework.NET ━ .NET Framework of rapid information system development 9.3 User management -Web part The user management module mainly manages the users who can log in to the system . Follow up work such as : The role of the user . The distribution of authority . What the user owns ...
- Safari Medium new Date() Format pit
I found out in the test today that , stay Chrome The following code in : new Date("2014-03-09"); stay Safari Error reported in invalid date. After consulting the information, we found a similar question and answer : st ...
- JVM Parameters （ One ）JVM Type and compiler mode
current JVM function Java Program ( And other compatible languages ) It's excellent in efficiency and stability . Adaptive memory management . garbage collection . Compile in time . Dynamic class loading . Lock the optimization —— Here's just a list of some amazing things that can happen in certain scenes , But they hardly ...
- linux tar.gz zip decompression Compress command
http://apps.hi.baidu.com/share/detail/37384818 download ADT link http://dl.google.com/android/ADT-0. ...
- mysql group by Usage analysis
group by Syntax can group query results according to each member of a given data column , Finally, we get a group summary table .SELECT Column names in Clauses must be grouped columns or column functions . Column functions for GROUP BY Each group defined by clause returns a result . ...
- Step control ——UIStepper
Step control , Can be used to replace the traditional text box used to enter values . Step control provides “+” and “-” Two buttons , To change stepper Inside value Increase or decrease in , The event called is UIControlEventValueChanged. from ...
- nanomsg How to write data to PipelineDB
nanomsg:https://github.com/nanomsg/nanomsg PipelineDB:https://github.com/pipelinedb/pipelinedb nanom ...
- Sphinx Learning notes （ One ）
Recently in charge of a project , Need to use full text retrieval , My environment is as follows : 1. Data saved in MySQL in 2. Need to support Chinese Retrieval 3. As simple as possible I chose Sphinx, as for solr and E ...