Reprint please indicate the source: Wang 亟亟 's way of Daniel
First of all here to wish you a Happy new year, work smoothly, fewer bugs!!!
This is in the Spring Festival holiday continued to maintain the progress of writing articles, but still lazy for a few days (played 4 days SC2 haha ha)
Today is about the Python article, after all, in the relatives of the family, do not bother to plug all kinds of mobile phone debugging what, and indeed a long time did not make Python, wrote, no more nonsense, start the subject!!
Function-Type programming
What is a function?
To decompose complex operations into simple functions as simple operations is the general concept of process-oriented, i.e. C implementations.
What is a functional formula?
function has no variable, any function, as long as the input is OK, the output is OK, with the function as the most basic component of programming called Functional Programming (traditional functional programming without variables, and Python)
In a Python variable declaration, you do not need to define a variable type like this
a=123
And like Java c these are
int a =123 ;
A variable in Python can also point to a method (function, as you call it to understand it).
Take the Max method here as an example
print(‘最大值函数 :‘max([12310020100
Returns the maximum value of 100, then we point the function to an object try
max;print(‘对象指向函数 最大值为:‘, maxValue(102020
So since the function can point to a variable, then the function can be passed as a parameter to another function? (guess) Let's try it!
This time, we'll experiment with absolute abs.
c = absdef testABS(a, b, c): return c(a) + bprint(‘测试函数结果:‘, testABS(-1, -9, c))结果:测试函数结果: -8
Which is 1-9=-8, which fits our vision, is the ability to pass in a function.
Map ()
The map method has 2 parameters, the first parameter is a function, the second argument is a iterable (so you pass in (), [] anything is OK)
Each result is generated separately, and the length of the new Iterable object returned is not reduced.
def testMAP(a): return a + aprint(‘返回的结果为:‘, list(map(testMAP, (12345))))结果:返回的结果为: [246810]
Similar implementations can be operated with a loop, like this
istValue = []forin [12345, ]: listValue.append(testMAP(x))print(‘返回的结果为:‘, listValue)
The result is the same as the map effect above
Another example of an int list to a string list
def toString(a): return str(a)print(‘转换为字符串:‘, list(map(toString, [1234])))结果:转换为字符串: [‘1‘‘2‘‘3‘‘4‘]
The original Iterable object has no relation to each element, and the reduce () function is the opposite
Reduce ()
Reduce is also passed two parameters the first parameter is a function, the second argument is a iterable, but the difference with map is that he continues the result and the next element of the sequence to do the cumulative calculation.
def Absall (A, b) : return ABS (a) + ABS ( b) Print (, reduce (Absall, (-1 , " Span class= "Hljs-number" >2 ,-3 ,-10 , Span class= "Hljs-number" >1 ))) Result: Returns the absolute value of all values and 17 process is 1 +2 =3 3 +3 =6 6 +10 =16 16 +1 =17
Then we combine the previous ToString operation (this time let him toint), calculate the sum, like this
# map reduce组合使用def toInt(a): return int(a)print(‘字符串转数字:‘, toInt("12"))print(‘组合拳结果为:‘, reduce(absAll, list(map(toInt, ["1""2""3"126
This way your program can become richer and easier to use.
Filter ()
The filter () function also needs to pass in 2 parameters, the first parameter is a function, and the second argument is a sequence. The filter iterates through each element and then, based on the logic of the first argument function, evaluates whether the current element is true or the false,true is added to the new sequence, and false is removed.
Let's look at the following example:
def checkValue(a): return50print(‘判断是否>5‘, list(filter(checkValue, [1693])))结果:判断是否>5 [69]
Returns a new sequence with a value that conforms to the first function logic.
Source: https://github.com/ddwhan0123/PythonExample/blob/master/Example/l6demo.py
Wang 亟亟 's Python Learning Path (eight)-functional programming, map (), reduce (), filter ()