The big topic with old Ziko python small function (2) _python

Source: Internet
Author: User
Tags iterable mul in python

The title of the last peace and speaking is "big topic small function", the so-called Big topic, is that these functions, if traced, will find something that sounds more tall. This way of thinking absolutely I firmly inherited the fine tradition of the Chinese nation. Since the celestial subjects saw the British start to play football, until now the so-called an erection of a country, have been trying to prove that the football originated in the front of the former dynasty before the time of a country, and also moved out of the time of a so-called high Qiu star to demonstrate that, of course, an erection of a country is unable to block the national team in the World Cup of impotence, can only use Gao Lai to make a fantasy. This way of thinking, I was firmly inherited, because in my growing up, it has been regarded as a fine tradition. Ah Q Originally is surnamed Zhao, and Zhao Master is the clan, than the scholar to long three generation, although was Zhao master hit the mouth.

Less nonsense, the book after the text, has studied map, below to see reduce.

I can't help but have some nonsense. Do not know reader is not heard of Mapreduc, if not, then Hadoop? If not, just google it. Here's what I've copied from Wikipedia to share.

Copy Code code as follows:

MapReduce is a software architecture proposed by Google for parallel operations in large datasets (larger than 1TB). The concepts "map" and "Reduce (Simplify)", and their main ideas, are borrowed from functional programming languages, as well as the features borrowed from the Vector programming language.

Do not know whether to understand, in short, you can use the idea of the beginning of a movie, the original today to tinker with the big data about this reduce. In any case, you have a dream feeling.

Reduce

Back to reality, sober up, continue to knock code:

Copy Code code as follows:

>>> reduce (lambda x,y:x+y,[1,2,3,4,5])
15

Please reader careful observation, whether you can see how the operation of it? Draw a diagram:

Do you remember how map was calculated? Forgot? Look at the code:

Copy Code code 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, 10]

Reader contrast, you will know the difference between the two. The original map is up and down, and reduce is the horizontal element of the operation.

The authoritative explanation comes from the official website:

Copy Code code 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). The left argument, X, are the accumulated value and the right argument, y, are the update value from the iterable. If the optional initializer is present, it's placed before the items of the iterable in the calculation, and serves as a Default when the iterable is empty. If initializer is isn't given and Iterable contains only one item, the ' the ' is returned. Roughly equivalent to:

Copy Code code as follows:

def reduce (function, iterable, Initializer=none):
it = iter (iterable)
If initializer is None:
Try
initializer = next (IT)
Except 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 you are doing the above to reduce by using our familiar for loop, you can do this:

Copy Code code 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 the universal, reduce is concise.

In order to exercise thinking, see such a problem, there are two list,a = [3,9,8,5,2],b=[1,4,9,2,6], calculated: a[0]b[0]+a1b1+ ... The results.

Copy Code code as follows:

>>> A
[3, 9, 8, 5, 2]
>>> b
[1, 4, 9, 2, 6]

>>> Zip (a,b) #复习一下zip, the following method uses the
[(3, 1), (9, 4), (8, 9), (5, 2), (2, 6)]

>>> sum (x*y for x,y in Zip (a,b)) #解析后直接求和
133

>>> new_list = [x*y for x,y in Zip (a,b)] #可以看做是上面方法的分布实施
>>> #这样解析也可以: new_tuple = (x*y for x,y in Zip (a,b))
>>> new_list
[3, 36, 72, 10, 12]
>>> sum (new_list) #或者: sum (new_tuple)
133

>>> reduce (lambda sum, (x,y): Sum+x*y,zip (A,b), 0) #这个方法是在耍酷呢吗?
133

>>> from operator import Add,mul #耍酷的方法也不止一个
>>> Reduce (Add,map (mul,a,b))
133

>>> reduce (lambda x,y:x+y, map (Lambda x,y:x*y, a,b)) #map, Reduce,lambda are all complete, cooler?
133

Filter

The Chinese meaning of filter is "filter", in Python, it acts as a filter. First look at the official note:

Copy Code code as follows:

Filter (function, iterable)

Construct a list from those elements of iterable for which function returns TRUE. Iterable May is either a sequence, a container which supports, or an iteration. If iterable is a string or a tuple, the result also has that type; Otherwise it is always a list. The If function is None, the identity function is assumed, which is, all elements of iterable that are false are removed.

Note This filter (function, iterable) is equivalent to [item as item in Iterable if function (item)] If function are not Non E and [item for item in Iterable if item] if the IF function is None.

This time it really does not translate (as if there was no translation in the past), but also do not explain the main points. Please be sure to read the above text yourself and understand what it means. English, no matter how stressed are not excessive, even if it is a beggar, say two English, may also be able to get the pound sterling dollars.

Through the following code experience:

Copy Code code 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] #与上面那句等效
[1, 2, 3, 4]

>>> filter (lambda c:c!= ' i ', ' Qiwsir ') #能不能对应上面文档说明那句话呢?
' QWSR ' # ' If iterable is a string or a tuple, the result also has that type;

At this point, a couple of small functions are introduced, which have no significant or stable expectations for the performance improvement of the program, but they are obvious to the simplicity of the code. Sometimes it can be used to show a show of Python elegance and play it cool.

Related Article

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.