Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional

16.01.2014 Views

CHAPTER 10 ■ BATTERIES INCLUDED 225 The heapreplace function is not quite as commonly used as the others. It pops the smallest element off the heap and then pushes a new element onto it. This is more efficient than a heappop followed by a heappush: >>> heapreplace(heap, 0.5) 0 >>> heap [0.5, 1, 5, 3, 2, 7, 9, 8, 4, 6] >>> heapreplace(heap, 10) 0.5 >>> heap [1, 2, 5, 3, 6, 7, 9, 8, 4, 10] The remaining two functions of the heapq module, nlargest(n, iter) and nsmallest(n, iter), are used to find the n largest or smallest elements, respectively, of any iterable object iter. You could do this by using sorting (for example, using the sorted function) and slicing, but the heap algorithm is faster and more memory-efficient (and, not to mention, easier to use). Deques (and Other Collections) Double-ended queues, or deques, can be useful when you need to remove elements in the order in which they were added. In Python 2.4, the collections module was added, which contains the deque type. ■Note As of Python 2.4, the collections module only contains the deque type. Possible future additions are B-trees and Fibonacci heaps. A deque is created from an iterable object (just like sets) and has several useful methods: >>> from collections import deque >>> q = deque(range(5)) >>> q.append(5) >>> q.appendleft(6) >>> q deque([6, 0, 1, 2, 3, 4, 5]) >>> q.pop() 5 >>> q.popleft() 6 >>> q.rotate(3) >>> q deque([2, 3, 4, 0, 1]) >>> q.rotate(-1) >>> q deque([3, 4, 0, 1, 2])

226 CHAPTER 10 ■ BATTERIES INCLUDED The reason for the usefulness of the deque is that it allows appending and popping efficiently at the beginning (to the left), as opposed to lists. As a nice side effect, you can also rotate the elements (that is, shift them to the right or left, wrapping around the ends) efficiently. Deque objects also have extend and extendleft methods, with extend working like the corresponding list method, and extendleft working analogously to appendleft. Note that the elements in the iterable used in extendleft will appear in the deque in reverse order. time The time module contains functions for, among other things, getting the current time, manipulating times and dates, and reading dates from strings and formatting dates as strings. Dates can be represented as either a real number (the seconds since 0 hours, January 1 in the “epoch,” a platform-dependent year; for UNIX, it’s 1970), or a tuple containing nine integers. These integers are explained in Table 10-5. For example, the tuple (2002, 1, 21, 12, 2, 56, 0, 21, 0) represents January 21, 2002, at 12:02:56, which is a Monday, and the 21st day of the year. (No daylight savings.) Table 10-5. The Fields of Python Date Tuples Index Field Value 0 Year For example, 2000, 2001, and so on 1 Month In the range 1–12 2 Day In the range 1–31 3 Hour In the range 0–23 4 Minute In the range 0–59 5 Second In the range 0–61 6 Weekday In the range 0–6, where Monday is 0 7 Julian day In the range 1–366 8 Daylight Savings 0, 1, or –1 Some of these values need some explaining: The range for seconds is 0–61 to account for leap seconds and double leap seconds. The Daylight Savings number is a Boolean value (true or false), but if you use –1, mktime (a function that converts such a tuple to a timestamp measured in seconds since the epoch) will probably get it right. Some of the most important functions in the time module are described in Table 10-6.

CHAPTER 10 ■ BATTERIES INCLUDED 225<br />

The heapreplace function is not quite as commonly used as the others. It pops the smallest<br />

element off the heap and then pushes a new element on<strong>to</strong> it. This is more efficient than a<br />

heappop followed by a heappush:<br />

>>> heapreplace(heap, 0.5)<br />

0<br />

>>> heap<br />

[0.5, 1, 5, 3, 2, 7, 9, 8, 4, 6]<br />

>>> heapreplace(heap, 10)<br />

0.5<br />

>>> heap<br />

[1, 2, 5, 3, 6, 7, 9, 8, 4, 10]<br />

The remaining two functions of the heapq module, nlargest(n, iter) and<br />

nsmallest(n, iter), are used <strong>to</strong> find the n largest or smallest elements, respectively, of any<br />

iterable object iter. You could do this by using sorting (for example, using the sorted function)<br />

and slicing, but the heap algorithm is faster and more memory-efficient (and, not <strong>to</strong> mention,<br />

easier <strong>to</strong> use).<br />

Deques (and Other Collections)<br />

Double-ended queues, or deques, can be useful when you need <strong>to</strong> remove elements in the<br />

order in which they were added. In <strong>Python</strong> 2.4, the collections module was added, which<br />

contains the deque type.<br />

■Note As of <strong>Python</strong> 2.4, the collections module only contains the deque type. Possible future additions<br />

are B-trees and Fibonacci heaps.<br />

A deque is created from an iterable object (just like sets) and has several useful methods:<br />

>>> from collections import deque<br />

>>> q = deque(range(5))<br />

>>> q.append(5)<br />

>>> q.appendleft(6)<br />

>>> q<br />

deque([6, 0, 1, 2, 3, 4, 5])<br />

>>> q.pop()<br />

5<br />

>>> q.popleft()<br />

6<br />

>>> q.rotate(3)<br />

>>> q<br />

deque([2, 3, 4, 0, 1])<br />

>>> q.rotate(-1)<br />

>>> q<br />

deque([3, 4, 0, 1, 2])

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

Saved successfully!

Ooh no, something went wrong!