I want to learn Python with my colleagues. I want to talk about small functions (2). I want to learn python.
The title of the previous lecture and this lecture is "big topic and small functions". The so-called big topic is that if these functions are traced back, they will find things that sound higher. This kind of thinking definitely inherits the fine traditions of the Chinese nation. Since the subjects of tianchao saw the British start to play football, until now the so-called a country's erection has been trying to prove that the football originated from the era of a country in front of the DPRK, and he also moved out of a star called Gao Yu to demonstrate that, of course, an erect country cannot stop the National Team's impotence on the World Cup journey and can only use Gao Yu to indulge in it. I firmly inherit this way of thinking, because it has always been regarded as a fine tradition during my growth. Ah Q was originally surnamed Zhao, and he and Zhao were his own family. He was three generations older than Xiucai, even though he was beaten by Zhao.
To put it bluntly, I have studied map in the previous article. Let's take a look at reduce.
I can't help but have to talk nonsense. I wonder if I have heard of MapReduc. If not, what about Hadoop? If not, google it. Here is my copy from Wikipedia.
Copy codeThe Code is as follows:
MapReduce is a software architecture proposed by Google. It is used for parallel operations on large-scale datasets (larger than 1 TB. The concepts "Map" and "Reduce", and their main ideas are borrowed from functional programming languages and features borrowed from Vector programming languages.
You don't have to worry about it. In short, you can start with the idea of obscenity. The reduce that we were going to drum up today is also related to big data. In any case, you just have a dream.
Reduce
Go back to reality, stay awake and continue coding:
Copy codeThe Code is as follows:
>>> Reduce (lambda x, y: x + y, [1, 2, 3, 4, 5])
15
Please take a closer look. Can you see how the computation works? Draw a picture:
Do you still remember how map is computed? Forgot? Check the Code:
Copy codeThe Code is as follows:
>>> List1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> List2 = [9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> Map (lambda x, y: x + y, list1, list2)
[10, 10, 10, 10, 10, 10, 10, 10]
You can see the differences between the two. In the past, map was an upper and lower operation, while reduce was an operation on elements one by one.
Authoritative explanations come from the official website:
Copy codeThe Code is as follows:
Reduce (function, iterable [, initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. for example, reduce (lambda x, y: x + y, [1, 2, 3, 4, 5]) calculates (1 + 2) + 3) + 4) + 5 ). the left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. if the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. if initializer is not given and iterable contains only one item, the first item is returned. roughly equivalent:
Copy codeThe Code is as follows:
Def reduce (function, iterable, initializer = None ):
It = iter (iterable)
If initializer is None:
Try:
Initializer = next (it)
Optional t StopIteration:
Raise TypeError ('reduce () of empty sequence with no initial value ')
Accum_value = initializer
For x in it:
Accum_value = function (accum_value, x)
Return accum_value
If we use a familiar for loop to do the reduce tasks above, we can do this:
Copy codeThe Code is as follows:
>>> Lst = range (1, 6)
>>> Lst
[1, 2, 3, 4, 5]
>>> R = 0
>>> For I in range (len (lst )):
... R + = lst [I]
...
>>> R
15
For is universal, and reduce is concise.
To exercise your thinking, there are two lists, a = [3, 9, 8, 5, 2], B = [, 9, 2, 6], and calculation: a [0] B [0] + a1b1 +....
Copy codeThe Code is as follows:
>>>
[3, 9, 8, 5, 2]
>>> B
[1, 4, 9, 2, 6]
>>> Zip (a, B) # review the zip file. Use the following method:
[(3, 1), (9, 4), (8, 9), (5, 2), (2, 6)]
>>> Sum (x * y for x, y in zip (a, B) # sum after Parsing
133
>>> New_list = [x * y for x, y in zip (a, B)] # It can be seen as the distribution implementation of the above method.
>>># This resolution can also be: new_tuple = (x * y for x, y in zip (a, B ))
>>> New_list
[3, 36, 72, 10, 12]
>>> Sum (new_list) # Or: sum (new_tuple)
133
>>> Reduce (lambda sum, (x, y): sum + x * y, zip (a, B), 0) # Is this method cool?
133
>>> From operator import add, mul # more than one cool Method
>>> Reduce (add, map (mul, a, B ))
133
>>> Reduce (lambda x, y: x + y, map (lambda x, y: x * y, a, B) # map, reduce, and lambda are all complete, cool?
133
Filter
The Chinese meaning of filter is "filter". In python, it is used as a filter. First, read the official instructions:
Copy codeThe Code is as follows:
Filter (function, iterable)
Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. if iterable is a string or a tuple, the result also has that type; otherwise it is always a list. if function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter (function, iterable) is equivalent to [item for item in iterable if function (item)] if function is not None and [item for item in iterable if item] if function is None.
I really don't translate this time (it seems that I haven't translated it in the past), and I don't want to explain the key points. Be sure to read the above text and understand its meaning. In English, no matter how hard it is to emphasize, even if it is a beggar, say two sentences in English, may be able to ask for a pound dollar.
Through the following code:
Copy codeThe Code is as follows:
>>> Numbers = range (-5, 5)
>>> Numbers
[-5,-4,-3,-2,-1, 0, 1, 2, 3, 4]
>>> Filter (lambda x: x> 0, numbers)
[1, 2, 3, 4]
>>> [X for x in numbers if x> 0] # equivalent to the above sentence
[1, 2, 3, 4]
>>> Filter (lambda c: c! = 'I', 'qiwsir ') # Can I explain the sentence in the above document?
'Qwsr' # "If iterable is a string or a tuple, the result also has that type ;"
So far, I have introduced several small functions in two cases. These functions have not been significantly or stably expected in terms of program performance improvement. However, they are obvious in the concise code. Sometimes it can be used to show the elegance of python and show your own cool.
Python 25 round (,) function usage
Yes, because 2. x and 3. x have been adjusted on the data type, depending on the content updated after 3. x.
There are many differences between 2. x and 3. x. It is recommended to learn 2. x first, because 3. x is not yet popular.
We also recommend that you do not view them together. This will be confusing.
Variable parameters of Python Functions
Def f (* paralist, ** paradict ):
For I in paralist:
Print I
For I in paradict:
Print I, paradict [I]