diff options
author | Raymond Hettinger <python@rcn.com> | 2004-05-12 20:55:56 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-05-12 20:55:56 (GMT) |
commit | 0e371f2cb6178de7344c97e4a546703c4bba140e (patch) | |
tree | 52fd07c90da0c60ee0c65fa991f7d27db49f00c3 /Doc/lib/libcollections.tex | |
parent | fd3f4fb7b114e177767568da35eee2241a8eda18 (diff) | |
download | cpython-0e371f2cb6178de7344c97e4a546703c4bba140e.zip cpython-0e371f2cb6178de7344c97e4a546703c4bba140e.tar.gz cpython-0e371f2cb6178de7344c97e4a546703c4bba140e.tar.bz2 |
Make sure "del d[n]" is properly supported. Was necessary because the
same method that implements __setitem__ also implements __delitem__.
Also, there were several good use cases (removing items from a queue
and implementing Forth style stack ops).
Diffstat (limited to 'Doc/lib/libcollections.tex')
-rw-r--r-- | Doc/lib/libcollections.tex | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/Doc/lib/libcollections.tex b/Doc/lib/libcollections.tex index 148ddea..c7d5c50 100644 --- a/Doc/lib/libcollections.tex +++ b/Doc/lib/libcollections.tex @@ -137,24 +137,21 @@ This section shows various approaches to working with deques. The \method{rotate()} method provides a way to implement \class{deque} slicing and deletion: +This pure python implementation of \code{del d[n]} shows how to use the +\method{rotate()} method as a building block for implementing a variety +of class{deque} operations: + \begin{verbatim} def delete_nth(d, n): - "del d[n]" d.rotate(-n) d.popleft() d.rotate(n) - ->>> d = deque('abcdef') ->>> delete_nth(d, 2) # remove the entry at d[2] ->>> d -deque(['a', 'b', 'd', 'e', 'f']) - \end{verbatim} -For slicing, the idea is the same. Use \method{rotate()} to bring a target -element to the left side of the deque. Remove old entries with -\method{popleft()}, add new entries with \method{extend()}, and then -reverse the rotation. +To implement \class{deque} slicing, use a similar approach applying +\method{rotate()} to bring a target element to the left side of the deque. +Remove old entries with \method{popleft()}, add new entries with +\method{extend()}, and then reverse the rotation. With minor variations on that approach, it is easy to implement Forth style stack manipulations such as \code{dup}, \code{drop}, \code{swap}, \code{over}, |