The map () function in Python
Tags: python
First look at the official documentation:
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 is applied to the items from all I Terables 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. Apply the ' function ' method to each element in the ' iterable ' of the iterated function and return the result as a list.
Cases:
例1:>>>def add(x):... return x+1>>>= [11,22,33]>>>map(add,aa)[122334]
As stated in the documentation, the MAP function maps the Add method to each element in AA, which calls the Add method on each element in AA and returns the list of results. It is important to note that the map function can have multiple iterative parameters, provided that the function method can receive these parameters. Otherwise it will be an error. Examples are as follows:
If more than one iterative parameter is given, the ' function ' is applied to the element ' parallel ' in each iteration parameter. That is, in each list, remove the element with the same subscript and execute ABC ().
Cases2:>>> defABC (A, B, c): ...returnA*10000 +B* - +C...>>>List1=[ One, A, -]>>>List2=[ -, -, the]>>>List3=[ the, the, About]>>> Map(ABC,LIST1,LIST2,LIST3) [114477,225588,336699]>>>A= Map(Ord,' ABCD ')>>> List(a) [ the,98, About, -]>>>A= Map(Ord,' ABCD ',' EFG ')# Two iterations are passed in, so the incoming function must be able to receive 2 parameters, and Ord cannot receive 2 parameters, so an error>>> List(a) Traceback (most recent): File"<pyshell#22>", line1,inch <Module> List(a)TypeError:Ord() takes exactly one argument (2Given>>> defF (A, B):returnA+B
When more than one iteration object is passed in, and their element length is inconsistent, the resulting iterator is only to the shortest length.
>>>=map(f,‘abcd‘,‘efg‘# 选取最短长度为3>>>list(a)[‘ae‘‘bf‘‘cg‘]
2. If ' function ' gives ' None ', then a default function is automatically invoked, see example:
Cases3:>>>List1=[ One, A, -]>>> Map(None, List1) [ One, A, -]>>>List1=[ One, A, -]>>>List2=[ -, -, the]>>>List3=[ the, the, About]>>> Map(None, LIST1,LIST2,LIST3) [( One, -, the), ( A, -, the), ( -, the, About)]
3. The last point to note is that the differences in map () between Python3 and Python2 (especially for users who go from py2 to Py3 are likely to be encountered):
In Python2, map will return the results directly, for example:
map(lambda x: x, [1,2,3])
can be returned directly
[1,2,3]
In Python3, however, a map object is returned:
<map object at 0x7f381112ad50>
If you want to get the result, you must use list to act on the map object.
Most importantly, if you do not precede map with the LIST,LAMBDA function, it will not execute at all.
The map () function in Python