Difference between revisions of "Map"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][checked revision]
(copyedit)
(Multiple-argument functions: copyedit)
 
(2 intermediate revisions by one user not shown)
Line 14: Line 14:
 
===Alternate forms===
 
===Alternate forms===
 
<code>map</code> has alternate forms:
 
<code>map</code> has alternate forms:
*<blockquote><source lang="python">map(fn, itr1, itr2, ...)</source></blockquote> When multiple iterables are offered, <code>fn</code> must take that many arguments. It will then apply the function to each of the iterables in parallel.
+
*<blockquote style="margin: 0;"><syntaxhighlight lang="python">map(fn, itr1, itr2, ...)</syntaxhighlight></blockquote> When multiple iterables are offered, <code>fn</code> must take that many arguments. It will then apply the function to each of the iterables in parallel.
*<blockquote><syntaxhighlight lang="python">map(None, itr)</syntaxhighlight></blockquote> In Python 2, when the given function is <code>None</code>, <code>map</code> assumes the identity function (<code>lambda x: x</code>). This only works for Python 2 — attempting this in Python 3 will result in a <code>'NoneType' object is not callable </code> error.
+
*<blockquote style="margin: 0;"><syntaxhighlight lang="python">map(None, itr)</syntaxhighlight></blockquote> In Python 2, when the given function is <code>None</code>, <code>map</code> assumes the identity function (<code>lambda x: x</code>). This only works for Python 2 — attempting this in Python 3 will result in a <code>'NoneType' object is not callable </code> error.
 
:If more than one iterable is provided, <code>map</code> will create [[tuple]]s using elements from each iterable in the final list (e.g, the first element of the first iterable and the first element of the second iterable will be combined in a tuple to form the first element of the returned list).
 
:If more than one iterable is provided, <code>map</code> will create [[tuple]]s using elements from each iterable in the final list (e.g, the first element of the first iterable and the first element of the second iterable will be combined in a tuple to form the first element of the returned list).
  
 
==Examples==
 
==Examples==
 
===Single-argument functions===
 
===Single-argument functions===
====<code>add_one</code>====
+
====<code>increment</code>====
For the first example, we will use the function <code>increment</code> and the 5-element <code>lst</code>:
+
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
def increment(x):
 
def increment(x):
  return x + 1
+
    return x + 1
 
lst = [1, 2, 3, 4, 5]
 
lst = [1, 2, 3, 4, 5]
</syntaxhighlight>
 
  
Mapping <code>increment</code> to <code>lst</code> in Python 2 would look like this:
+
# Python 2
<syntaxhighlight lang="python">
+
 
>>> map(increment, lst)
 
>>> map(increment, lst)
 
[2, 3, 4, 5, 6]  
 
[2, 3, 4, 5, 6]  
</syntaxhighlight>
 
  
To do the same in Python 3, we convert the result from a map object to a list:
+
# Python 3
<syntaxhighlight lang="python">
+
 
>>> list(map(increment, lst)
 
>>> list(map(increment, lst)
 
[2, 3, 4, 5, 6]
 
[2, 3, 4, 5, 6]
Line 41: Line 36:
  
 
====<code>double</code>====
 
====<code>double</code>====
Let's use another function, <code>double</code> and a list <code>lst</code>:
 
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
def double(x):
 
def double(x):
  return x * 2
+
    return x * 2
 
lst = [1, 3, 5, 7, 9]
 
lst = [1, 3, 5, 7, 9]
</syntaxhighlight>
 
  
Applying <code>map</code> on this list:
 
<syntaxhighlight lang="python">
 
 
# Python 2
 
# Python 2
 
>>> map(double, lst)
 
>>> map(double, lst)
Line 60: Line 51:
  
 
===Multiple-argument functions===
 
===Multiple-argument functions===
Now we'll try a few examples where <code>map</code> takes in a multiple-argument function and multiple iterables.
 
 
 
=====<code>add_two_things</code>=====
 
=====<code>add_two_things</code>=====
In this example, we use a function, <code>add_two_things</code>, which does exactly as its name suggests. We also create two lists: <code>lst1</code> and <code>lst2</code>:
 
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
def add_two_things(x, y):
 
def add_two_things(x, y):
Line 71: Line 59:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Using the <code>map</code> function with these arguments will combine items from matching indices of the two lists:
+
Using the <code>map</code> function with the above arguments will combine items from matching indices of the two lists:
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
# Python 2
 
# Python 2
Line 83: Line 71:
  
 
=====<code>multiply_by_each_other</code>=====
 
=====<code>multiply_by_each_other</code>=====
Suppose we wanted to multiply the elements of two lists by each other. <code>map</code> would be an incredibly convenient way to do so! We'll define our lists, <code>lst1</code> and <code>lst2</code>, and our multiplication function, <code>multiply_by_each_other</code>:
+
Suppose we wanted to multiply the elements of two lists by each other.
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
def multiply_by_each_other(x, y):
 
def multiply_by_each_other(x, y):
Line 91: Line 79:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Using <code>map</code>:
+
We can use the above function with <code>map</code>:
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
# Python 2
 
# Python 2

Latest revision as of 10:45, 4 July 2014

map is a built-in Python higher-order function that applies a given function to all the items of an iterable. The returned result differs between Python 2 and Python 3:

  • In Python 2, map returns a list, regardless of the type of iterable passed.
  • In Python 3, map returns an iterable map object, which can be converted into a sequence using the appropriate constructor (e.g., list(...), tuple(...)).

Forms

General form

The most general form of the map function is:

map(fn, itr)

where the function being applied is fn and the lone iterable is itr. fn must be a one-argument function.

Alternate forms

map has alternate forms:

  • map(fn, itr1, itr2, ...)
    When multiple iterables are offered, fn must take that many arguments. It will then apply the function to each of the iterables in parallel.
  • map(None, itr)
    In Python 2, when the given function is None, map assumes the identity function (lambda x: x). This only works for Python 2 — attempting this in Python 3 will result in a 'NoneType' object is not callable error.
If more than one iterable is provided, map will create tuples using elements from each iterable in the final list (e.g, the first element of the first iterable and the first element of the second iterable will be combined in a tuple to form the first element of the returned list).

Examples

Single-argument functions

increment

def increment(x):
    return x + 1
lst = [1, 2, 3, 4, 5]
 
# Python 2
>>> map(increment, lst)
[2, 3, 4, 5, 6] 
 
# Python 3
>>> list(map(increment, lst)
[2, 3, 4, 5, 6]

double

def double(x):
    return x * 2
lst = [1, 3, 5, 7, 9]
 
# Python 2
>>> map(double, lst)
[2, 6, 10, 14, 18]
 
# Python 3
>>> list(map(double, lst))
[2, 6, 10, 14, 18]

Multiple-argument functions

add_two_things
def add_two_things(x, y):
   return x + y
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]

Using the map function with the above arguments will combine items from matching indices of the two lists:

# Python 2
>>> map(add_two_things, lst1, lst2)
[5, 7, 9]
 
# Python 3
>>> list(map(add_two_things, lst1, lst2))
[5, 7, 9]
multiply_by_each_other

Suppose we wanted to multiply the elements of two lists by each other.

def multiply_by_each_other(x, y):
   return x * y
lst1 = [2, 4, 6]
lst2 = [5, 8, 10]

We can use the above function with map:

# Python 2
>>> map(multiply_by_each_other, lst1, lst2)
[10, 32, 60]
 
# Python 3
>>> list(map(multiply_by_each_other, list_a, list_b))
[10, 32, 60]

Using None as the function (only for Python 2)

Using None as the function is analogous to using the identity function.

A single iterable

In the case of a single list, map will simply return the identity of the list. We define lst and then pass it into map:

>>> lst = [7, 12, 33]
>>> map(None, lst)
[7, 12, 33]
Multiple iterables

Multiple iterables can passed as arguments along with None into the map function. We create three lists:

lst1 = [1, 2, 3, 4]
lst2 = [5, 7, 9, 11]
lst3 = [12, 14, 16, 18]

Using map:

>>> map(None, lst1, lst2, lst3)
[(1, 5, 12), (2, 7, 14), (3, 9, 16), (4, 11, 18)]

The first element from each list is combined into a tuple in the resulting list. The second tuple is a combination of the second elements, and so on.

Sources