In many situations you would like to apply the same function to lots of different pieces of data. For example, let's create two arrays of numbers, and use our add
function to add pairs of numbers together. In the Python Console type:
def add(x, y):
"""Simple function returns the sum of the arguments"""
return x + y
def multiply(x, y):
"""
Simple function that returns the product of the
two arguments
"""
return x * y
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
result = []
for i, j in zip(a, b):
result.append(add(i, j))
print(result)
The above code has looped over every pair of numbers in the lists a
and b
, and has called the function add
for each pair. Each result is appended to the list result
, which is printed at the end of the loop.
Applying the same function to every item in a list (or pair of lists) of data is really common. For example, in a molecular simulation, you may want to loop over a list of every molecule and call a calculate_energy
function for each one. In a fluid dynamics simulation, you may want to loop over a list of grid points and call a solve_gridpoint
function for each one. This pattern, of calling the same function for each element of a list (or set of lists) of data, is called mapping. In the above example, we have mapped the function add
onto the lists a
and b
, returning result
.
The above code mapped the function add
. How about if we wanted to map our diff
or multiply
functions? One option would be to copy out this code again. A better solution would be to use functional programming to write our own mapping function.
def mapper(func, arg1, arg2):
"""
This will map the function 'func' to each pair
of arguments in the list 'arg1' and 'arg2', returning
the result
"""
res = []
for i, j in zip(arg1, arg2):
res.append(func(i, j))
return res
result = mapper(add, a, b)
print(result)
Now type
result = mapper(multiply, a, b)
print(result)
Can you see how this works?
The mapper
function takes as its first argument the function to be mapped. The other arguments are the two lists of data for the mapping. The part
zip(arg1, arg2)
takes the two arguments and returns an interator which can go through them both at the same time. As soon as one of them runs out of elements, it will stop. The mapper
function then loops through each of these pairs of data, calling func
for each pair, and storing the result in the list res
. This is then returned at the end.
Because the mapper
function calls the mapped function using the argument func
, it can map any function that is passed to it, as long as that function accepts two arguments. For example, let us now create a completely different function to map:
import math
def calc_distance(point1, point2):
"""
Function to calculate and return the distance between
two points
"""
dx2 = (point1[0] - point2[0]) ** 2
dy2 = (point1[1] - point2[1]) ** 2
dz2 = (point1[2] - point2[2]) ** 2
return math.sqrt(dx2 + dy2 + dz2)
This has created a function that calculates the distance between two points. Let’s now create two lists of points and use mapper
to control the calculation of distances between points:
points1 = [(1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0)]
points2 = [(4.0, 4.0, 4.0), (5.0, 5.0, 5.0), (6.0, 6.0, 6.0)]
distances = mapper(calc_distance, points1, points2)
print(distances)
Mapping is so common and useful that it is built in as a standard Python function, called map
. For example:
distances = map(calc_distance, points1, points2)
print(distances)
This is perhaps a little unexpected as Python hasn’t actually given us the answer. Instead, the built-in map
function has returned an object which is ready and waiting to perform the calculation you’ve asked. This can be useful because by evaluating the map “lazily”, you can avoid unnecessary computation. The technical term for the thing that has been returned is an iterator. You can use this object in a for
loop just fine but you can only loop over it once.
If you want to force Python to evaluate the map and give you the answers, you can turn it into a list usig the list
function:
list(distances)
You should see that your calc_distances
function has been mapped to all of the pairs of points.
The standard map
function behaves very similar to your hand-written mapper
function, returing an iterator containing the result of applying your function to each item of data.
One advantage of map
is that it knows how to handle multiple arguments. For example, let’s create a function that only maps a single argument:
def square(x):
"""
Simple function to return the square of
the passed argument
"""
return x * x
Now, let’s try to use your handwritten mapper
function to map square
onto a list of numbers:
numbers = [1, 2, 3, 4, 5]
result = mapper(square, numbers)
This raises an exception since we wrote our mapper
function so that it mapped functions that expected two arguments. That meant that our mapper
function needs three arguments; the mapped function plus two lists of arguments.
The standard map function can handle different numbers of arguments:
result = map(square, numbers)
print(list(result))
The standard map
function can work with mapping functions that accept any number of arguments. If the mapping function accepts n
arguments, then you must pass n+1
arguments to map, i.e. the mapped function, plus n
lists of arguments:
def find_smallest(arg1, arg2, arg3):
"""
Function used to return the smallest value out
of 'arg1', 'arg2' and 'arg3'
"""
return min(arg1, arg2, arg3)
a = [1, 2, 3, 4, 5]
b = [5, 4, 3, 2, 1]
c = [1, 2, 1, 2, 1]
result = map(find_smallest, a, b, c)
print(list(result))
Is this output what you expect?
Download and unpack the file shakespeare.tar.bz2
, e.g. type into a Python Console:
import urllib
import tarfile
urllib.request.urlretrieve("https://gitlab.com/milliams/parallel_python/raw/master/shakespeare.tar.bz2", "shakespeare.tar.bz2")
with tarfile.open("shakespeare.tar.bz2") as tar:
tar.extractall()
This has created a directory called shakespeare
that contains the full text of many of Shakespeare’s plays.
Your task is to write a Python script, called countlines.py
, that will count the total number of lines in each of these Shakespeare plays, e.g. by using the command line call
python countlines.py shakespeare
To do this, first write a function that counts the number of lines in a file.
Then, use the standard map
function to count the number of lines in each Shakespeare play, printing the result as a list.
If you get stuck or want some inspiration, a possible answer is given here.