Python-functional programming, map/reduce,filter and sorted

Source: Internet
Author: User

What is functional programming?

    • Programming paradigms parallel to object-oriented programming (object-oriented programming) and procedural programming (procedural programming).
    • The main feature is that the function is the first class citizen, can be defined inside and outside the function, as a function parameter or return value, a combination of functions.
    • Emphasizing the decomposition of the computational process into reusable functions, the typical example is the map combination of methods and reduce methods into the MapReduce algorithm.
    • Only pure, no side effects of the function, is the qualified function.

Know-what is functional programming thinking?

The biggest difference between functional programming and imperative programming is that:

Functional programming concerns the mapping of data, imperative programming is concerned with the steps to solve the problem.

So functional programming is the most important thing is the mapping of data, to use mathematical thinking to solve problems, rather than computer instructions.

Map/reduce

The map () function accepts a function, one or more iterated objects, which function on each element of the iteration object and return it as an iterator.

 def  ABS (x):  if x > 0:  return  x return -x
#map () Return iterator, lazy, need list to convert a = List (map (abs,[-1,-6,7,10]))>>>a[1,6,7,10]

# # #求两个list元素的对应乘积返回list
def Sub (x, y):
      return x * y
A = List (map (sub,[1,2,3],[4,5,6))
>>>A
[4,10,18]

Reduce

Python3 has removed reduce () from the global to use the need to import from functions and tools

>>>from functools Import Reduce

The function accepted by the reduce function must have two arguments, and the other is a list or a tuple

Take two elements from an element to accumulate operations

 from Import Reduce def Add (x, y    ): return x += reduce (add,[1,2,3,4,5])>>>a15

You can also convert a list or a tuple to an integer

 from Import Reduce def tra (x, y    ): return x*10 += reduce (tra, (1,2,3,4,5))>>>a12345

Map () and reduce () work with

 from Import Reduce def sq (x):     return x *def  Add (y,z):    return y += reduce (Add,map ( sq,[1,2,3]) >>>a14

Filter filters

The filter () takes a function, a sequence, which functions sequentially on each element, leaving the ture discarded false

# # #只保留正数
def filt (x): if x > 0: return= List (Map (filt,[-1,- 2,3,4]) >>>a[3,4]

Sorted

Sorted (iterable, Key=none, Reverse=false)

The sorted () function is also a high-order function, and key can accept a function to return on each element

Sorted ([2,4,1,3,7]) [1,2,3,4,7]#Key accepts function >>>sorted ([1,-5,3,-6],key= ABS) [-6,-5,1,3]

#可以传入第三参数reverse =ture, the default positive order is false to implement reverse order
>>>sorted ([1,3,5,7,9]reverse=ture)
[9,7,5,3,1]

##### #sort函数
A = [1,0,3,5,4]
A.sort ()
>>>print (a)
[0,1,3,4,5]
#倒序
A = [1,0,3,5,4]
A.sort (reverse = True)
>>>print (a)
[5,4,3,1,0]
If you need a list copy, do not use the assignment method so that the resulting copy is the original list, pointing to the same address in memory
To use a slice operation to get a new copy

Python-functional programming, map/reduce,filter and sorted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.