Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6ad32b89
Commit
6ad32b89
authored
May 09, 2004
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add more examples.
parent
6e4401f0
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
1 deletion
+103
-1
Doc/lib/libcollections.tex
Doc/lib/libcollections.tex
+52
-1
Lib/test/test_deque.py
Lib/test/test_deque.py
+51
-0
No files found.
Doc/lib/libcollections.tex
View file @
6ad32b89
...
@@ -130,6 +130,35 @@ IndexError: pop from an empty deque
...
@@ -130,6 +130,35 @@ IndexError: pop from an empty deque
deque(['c', 'b', 'a'])
deque(['c', 'b', 'a'])
\end{verbatim}
\end{verbatim}
\subsection
{
Recipes
\label
{
deque-recipes
}}
This section shows various approaches to working with deques.
The
\method
{
rotate()
}
method provides a way to implement
\class
{
deque
}
slicing and deletion:
\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.
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
}
,
\code
{
pick
}
,
\code
{
rot
}
, and
\code
{
roll
}
.
A roundrobin task server can be built from a
\class
{
deque
}
using
A roundrobin task server can be built from a
\class
{
deque
}
using
\method
{
popleft()
}
to select the current task and
\method
{
append()
}
\method
{
popleft()
}
to select the current task and
\method
{
append()
}
...
@@ -147,7 +176,7 @@ def roundrobin(*iterables):
...
@@ -147,7 +176,7 @@ def roundrobin(*iterables):
pending.append(task)
pending.append(task)
>>> for value in roundrobin('abc', 'd', 'efgh'):
>>> for value in roundrobin('abc', 'd', 'efgh'):
print value
...
print value
a
a
d
d
...
@@ -159,3 +188,25 @@ g
...
@@ -159,3 +188,25 @@ g
h
h
\end{verbatim}
\end{verbatim}
Multi-pass data reduction algorithms can be succinctly expressed and
efficiently coded by extracting elements using multiple calls to
\method
{
popleft()
}
, applying the reduction function, and using
\method
{
append()
}
for adding the result back to the queue.
For example, building a balanced binary tree of nested lists entails
reducing two adjacent nodes into one by grouping them in a list:
\begin{verbatim}
def maketree(iterable):
d = deque(iterable)
while len(d) > 1:
pair = [d.popleft(), d.popleft()]
d.append(pair)
return list(d)
>>> print maketree('abcdefgh')
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
\end{verbatim}
Lib/test/test_deque.py
View file @
6ad32b89
...
@@ -474,6 +474,57 @@ IndexError: pop from an empty deque
...
@@ -474,6 +474,57 @@ IndexError: pop from an empty deque
>>> d
>>> d
deque(['c', 'b', 'a'])
deque(['c', 'b', 'a'])
>>> 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'])
>>> def roundrobin(*iterables):
... pending = deque(iter(i) for i in iterables)
... while pending:
... task = pending.popleft()
... try:
... yield task.next()
... except StopIteration:
... continue
... pending.append(task)
...
>>> for value in roundrobin('abc', 'd', 'efgh'):
... print value
...
a
d
e
b
f
c
g
h
>>> def maketree(iterable):
... d = deque(iterable)
... while len(d) > 1:
... pair = [d.popleft(), d.popleft()]
... d.append(pair)
... return list(d)
...
>>> print maketree('abcdefgh')
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
"""
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment