The design of MapReduce is inspired by functional programming, which is not intended to mention MapReduce, and takes the map () function in Python to learn. # from https://my.oschina.net/zyzzy/blog/115096
The documentation is described here:
Map (function, iterable, ...)
Apply function to every item of iterable and return a list of the results. If additional iterable arguments is passed,function must take that many arguments and was applied to the Items from all iterables in parallel. If one iterable is shorter than another it's assumed to be extended withnoneitems. If function isnone, the identity function is assumed; If there is multiple arguments, map () returns a list consisting of tuples containing the corresponding items from all ITE Rables (a kind of transpose operation). The iterable arguments is a sequence or any iterable object; The result is always a list.
1.1-point View:
1. Apply the ' function ' method to each element in the ' iterable ' of the iterated function and return the result as a list.
Here's an example:
>>> def add100< Span class= "hljs-function" > (x) : ... return x+100 ... >>> hh = [11,22,33]>>> map (add100,hh) [111, 122, 133]
As the document says: Add100 the elements in HH and returns a list of the results.
2. If an additional iterative parameter is given, the ' function ' is applied to the element ' parallel ' in each iteration parameter. (translation is not good, the key here is ' parallel ')
>>>Def Abc (A, B, c) : ... return a*10000 + b*100 + C ... >>> list1 = [11,22,33]>>> list2 = [44,55,66]>>> list3 = [ 77,88,99] >>> map (ABC,LIST1,LIST2,LIST3) [114477, 225588, 336699]
See the parallel effect now! In each list, an element with the same subscript is removed, and ABC () is executed.
3, if ' function ' gives is ' None ', automatically assumes an ' identity ' function (this ' identity ' does not know how to explain, see example Bar)
>>> List1 = [11,22,638>>> map (NONE,LIST1) [11,22, 33]>>> list1 = [11,22,33]> >> list2 = [44,55,66]>>> list3 = [77,88,99]>>> map (NONE,LIST1,LIST2,LIST3) [(11, 44, 77), (22, 55, 88), (33, 66, 99)]
It seems a bit awkward to explain in words, and examples should be easy to understand.
The introduction to here should be almost there! But there's something to dig up:
Someone on the StackOverflow said you could understand this map ():
map(f, iterable)基本上等于:[f(x) for x in iterable]
Try it quickly:
>>>Def add100< Span class= "hljs-function" > (x) : ... return x + 100 ... >>> list1 = [11,22,33]>>> map (add100,list1) [101, Span class= "Hljs-number" >102, 103]>>> [add100 (i) for i in list1][101, 102, 103]
Oh, the output is the same. The original map () is the list derivation Ah! It's wrong to think like this: it's just a superficial phenomenon! Let's take another example:
>>>Def Abc (A, B, c) : ... return a*10000 + b*100 + C ... >>> list1 = [11,22,33]>>> list2 = [44,55,66]>>> list3 = [ 77,88,99] >>> map (ABC,LIST1,LIST2,LIST3) [114477, 225588, 336699]
This example we have seen above, if the use of a list deduction should be how to write it? I think so:
[abc(a,b,c) for a in list1 for b in list2 for c in list3]
But seeing the results, we found that this was not the case at all:
[114477,114488,114499,115577, 115588, 115599, 116677, 116688, 116699, 224477, 224488, 224499, 225577, 225588, 225599, 226677, 226688, 226699, 334477, 334488, 334499, 335577, 335588, 335599, 336677, 336688, 336699]
This is the result of the above list derivation. Why is it so much? Of course, the list derivation can be written like this:
result = []for a in list1: for b in list2: for c in list3: result.append(abc(abc))
So, if the three list is considered a matrix:
| 11 |
22 |
33 |
| 44 |
55 |
66 |
| 77 |
88 |
99 |
Map () does only the operations above the column, and the list derivation (that is, nested for loop) does the Cartesian product.
OK, just write it down here. Only personal understanding, if there are mistakes please correct me, thank you!
Some of the examples above are from here:
Http://infohost.nmt.edu/tcc/help/pubs/python/web/map-function.html
Http://stackoverflow.com/questions/10973766/understanding-the-map-function-python
Python map function