Difference between revisions of "Map"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][checked revision]
m (Alternate forms: fix formatting)
m (Single-argument functions: just code)
Line 20: Line 20:
 
==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)

Revision as of 10:43, 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

Now we'll try a few examples where map takes in a multiple-argument function and multiple iterables.

add_two_things

In this example, we use a function, add_two_things, which does exactly as its name suggests. We also create two lists: lst1 and lst2:

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

Using the map function with these 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. map would be an incredibly convenient way to do so! We'll define our lists, lst1 and lst2, and our multiplication function, multiply_by_each_other:

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

Using 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