Difference between revisions of "Map"

From CS 61A Wiki
Jump to: navigation, search
[checked revision][checked revision]
m (minor copyedit)
(Multiple-argument functions: copyedit)
 
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:<code><span style="text-transform:lowercase">m</span>ap</code>}}
+
{{DISPLAYTITLE:map}}
'''<code>map</code>''' is a [https://docs.python.org/2/library/functions.html built-in], [http://composingprograms.appspot.com/pages/16-higher-order-functions.html higher-order], [[list comprehension|list-comprehensive]] function available in the Python interpreter.
+
{{C-class}}
 
+
'''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:
Given a function and an iterable (often in the form of a [[list]]), <code>Map</code> works by (1) applying the function to each element of the iterable and (2) returning the result.
+
* In Python 2, <code>map</code> returns a [[list]], regardless of the type of iterable passed.
 
+
* In Python 3, <code>map</code> returns an iterable ''map object'', which can be converted into a [[sequence]] using the appropriate constructor (e.g., <code>list(...)</code>, <code>tuple(...)</code>).
The [https://docs.python.org/2/library/functions.html#map Python 2.7 map built-in] will always return a ''list'', regardless of the type of iterable passed. The [https://docs.python.org/3.2/library/functions.html#map Python 3.2+ map built-in] will return a ''map-object'' which can easily be converted into a list.  
+
  
 
==Forms==
 
==Forms==
===General Form===
+
===General form===
In Python, the most general form of the <code>map</code> function is as follows:
+
The most general form of the <code>map</code> function is:
 
<syntaxhighlight lang="python">map(fn, itr)</syntaxhighlight>
 
<syntaxhighlight lang="python">map(fn, itr)</syntaxhighlight>
  
where the function being applied is <code>fn</code> and the lone iterable is <code>itr</code>.
+
where the function being applied is <code>fn</code> and the lone iterable is <code>itr</code>. <code>fn</code> must be a one-argument function.
  
===Alternate Forms===
+
===Alternate forms===
In addition to the most general form, <code>map</code> can take many different shapes. The following are a few examples of these alternate forms and the assumptions they carry.
+
<code>map</code> has alternate forms:
 +
*<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 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).
  
<syntaxhighlight lang="python">map(fn, itr1, itr2, ...)</syntaxhighlight>
+
==Examples==
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.
+
===Single-argument functions===
 +
====<code>increment</code>====
 +
<syntaxhighlight lang="python">
 +
def increment(x):
 +
    return x + 1
 +
lst = [1, 2, 3, 4, 5]
  
<syntaxhighlight lang="python">map(None, itr)</syntaxhighlight>
+
# Python 2
When the given function is <code>None</code>, <code>map</code> assumes the [https://docs.python.org/2/library/functions.html#id identity] function.
+
>>> map(increment, lst)
If more than one iterable is offered and the given function is <code>None</code>, <code>map</code> will create [[tuple|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 <code>map</code>'s resultant list.)
+
[2, 3, 4, 5, 6]  
  
==Examples==
+
# Python 3
===Single argument functions===
+
>>> list(map(increment, lst)
====<code>add_one</code>====
+
[2, 3, 4, 5, 6]
For the first example, we will use the function <code>add_one</code> and the basic 5-element list <code>my_list</code> which we will define as follows:
+
</syntaxhighlight>
 +
 
 +
====<code>double</code>====
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
def add_one(x):
+
def double(x):
  return x+1
+
    return x * 2
 +
lst = [1, 3, 5, 7, 9]
  
my_list = [1, 2, 3, 4, 5]
+
# Python 2
 +
>>> map(double, lst)
 +
[2, 6, 10, 14, 18]
 +
 
 +
# Python 3
 +
>>> list(map(double, lst))
 +
[2, 6, 10, 14, 18]
 
</syntaxhighlight>
 
</syntaxhighlight>
Mapping <code>add_one</code> to <code>my_list</code> in Python 2.7 would look something like this:
+
 
 +
===Multiple-argument functions===
 +
=====<code>add_two_things</code>=====
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
>>>map(add_one, my_list)
+
def add_two_things(x, y):
[2, 3, 4, 5, 6]  
+
  return x + y
 +
lst1 = [1, 2, 3]
 +
lst2 = [4, 5, 6]
 
</syntaxhighlight>
 
</syntaxhighlight>
If we wanted to do the same in Python 3.2 or later, we would simply convert the result from a map object to a list, and it would look something like this:
+
 
 +
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">
>>>list(map(add_one, my_list)
+
# Python 2
[2, 3, 4, 5, 6]
+
>>> map(add_two_things, lst1, lst2)
 +
[5, 7, 9]
 +
 
 +
# Python 3
 +
>>> list(map(add_two_things, lst1, lst2))
 +
[5, 7, 9]
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
=====<code>multiply_by_each_other</code>=====
 +
Suppose we wanted to multiply the elements of two lists by each other.
 +
<syntaxhighlight lang="python">
 +
def multiply_by_each_other(x, y):
 +
  return x * y
 +
lst1 = [2, 4, 6]
 +
lst2 = [5, 8, 10]
 +
</syntaxhighlight>
  
====<code>multiply_by_two</code>====
+
We can use the above function with <code>map</code>:
Let's use another function, <code>multiply_by_two</code> (which we could have named <code>double</code>!), and a new list, <code>new_list</code>.
+
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
def multiply_by_two(x):
+
# Python 2
  return x*2
+
>>> map(multiply_by_each_other, lst1, lst2)
 +
[10, 32, 60]
  
new_list = [1, 3, 5, 7, 9]
+
# Python 3
 +
>>> list(map(multiply_by_each_other, list_a, list_b))
 +
[10, 32, 60]
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Just as in the <code>add_one</code> example, we have to adjust our code depending on the version of Python we want to use.  
+
===Using <code>None</code> as the function (only for Python 2)===
Applying <code>map</code> in Python 2.7 and using our <code>multiply_by_two</code> function and <code>new_list</code> gives us the following:
+
Using <code>None</code> as the function is analogous to using the <code>identity</code> function.
 +
 
 +
=====A single iterable=====
 +
In the case of a single list, <code>map</code> will simply return the identity of the list. We define <code>lst</code> and then pass it into <code>map</code>:
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
>>>map(multiply_by_two, new_list)
+
>>> lst = [7, 12, 33]
[2, 6, 10, 14, 18]
+
>>> map(None, lst)
 +
[7, 12, 33]
 
</syntaxhighlight>
 
</syntaxhighlight>
  
As before, a change to Python 3.3 simply involves converting the ''map object'' to a more user-friendly ''list''.
+
=====Multiple iterables=====
 +
Multiple iterables can passed as arguments along with <code>None</code> into the <code>map</code> function. We create three lists:
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
>>>list(map(multiply_by_two, new_list))
+
lst1 = [1, 2, 3, 4]
[2, 6, 10, 14, 18]
+
lst2 = [5, 7, 9, 11]
 +
lst3 = [12, 14, 16, 18]
 +
</syntaxhighlight>
 +
 
 +
Using <code>map</code>:
 +
<syntaxhighlight lang="python">
 +
>>> map(None, lst1, lst2, lst3)
 +
[(1, 5, 12), (2, 7, 14), (3, 9, 16), (4, 11, 18)]
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
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.
  
===Multiple argument functions===
+
== Sources ==
=====<code>ex1</code>=====
+
* https://docs.python.org/2/library/functions.html#map
=====<code>ex2</code>=====
+
* https://docs.python.org/3.2/library/functions.html#map
===Using <code>None</code> as a function===
+
=====<code>ex1</code>=====
+
=====<code>ex2</code>=====
+

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