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 :

map(function_to_apply, list_of_inputs)

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:
squared.append(i**2)

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 :

def multiply(x):
return (x*x)
def add(x):
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))
print(less_than_zero)

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 :

1 http://book.pythontips.com/en/latest/map_filter.html

python Library function Map, Filter and Reduce More related articles on the usage of

  1. [ 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 ...

  2. 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_ ...

  3. 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. ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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() - ...

  8. 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 ...

  9. [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 >& ...

Random recommendation

  1. Fullscreen API Full screen web page

    Refer to the documentation :http://blog.csdn.net/tywali/article/details/8623938 Script code <script type="text/javascript ...

  2. Unity The road of mobile travel &lt; 11、 ... and &gt; 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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. ...

  7. 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 . ...

  8. 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 ...

  9. nanomsg How to write data to PipelineDB

    nanomsg:https://github.com/nanomsg/nanomsg PipelineDB:https://github.com/pipelinedb/pipelinedb nanom ...

  10. 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 ...