04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 6 ■ ABSTRACTION 135<br />

>>> numbers = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]<br />

>>> filter(lambda n: n % 2 == 0, numbers)<br />

[72, 108, 108, 44, 32, 114, 108, 100]<br />

The lambda expression simply checks whether the remainder of a given number when<br />

divided by 2 is zero (which is another way of saying that the number is even).<br />

Now, map and filter can be very useful, but they were added to the language before list<br />

comprehension came along. If you think about it, anything that map and filter can accomplish<br />

can also be done with list comprehensions:<br />

>>> [chr(n) for n in numbers] # characters corresponding to numbers<br />

['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!']<br />

>>> [ord(c) for c in 'Hello, world!'] # numbers corresponding to characters<br />

[72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]<br />

>>> [n for n in numbers if n % 2 == 0] # filters out the odd numbers<br />

[72, 108, 108, 44, 32, 114, 108, 100]<br />

In my opinion, list comprehensions are, in most cases, more readable than using map and<br />

filter. I won’t go so far as to say that you always should use list comprehensions: it’s largely a<br />

matter of taste, and the demands of each specific programming task.<br />

■Note If it is speed you are after, you may want to go with map and filter after all. When used with builtin<br />

functions, they are faster than list comprehensions.<br />

reduce<br />

But what about the third function, reduce? This is a tricky one, and I confess that I rarely use it.<br />

But people used to functional programming may find it useful. It combines the first two elements<br />

of a sequence with a given function, and combines the result with the third element, and so on<br />

until the entire sequence has been processed and a single result remains. For example, if you<br />

wanted to sum all the numbers of a sequence, you could use reduce with lambda x, y: x+y (still<br />

using the same numbers): 2<br />

>>> numbers = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]<br />

>>> reduce(lambda x, y: x+y, numbers)<br />

1161<br />

In this example, all the numbers of numbers are summed by successively adding the current<br />

sum and the next number in the sequence. What actually happens is very close to this:<br />

sum = 0<br />

for number in numbers:<br />

sum = sum + number<br />

2. Actually, instead of this lambda function, you could import the function add from the operator module,<br />

which has a function for each of the built-in operators. Using functions from the operator module is<br />

always more efficient than using your own functions.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!