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
1749a135
Commit
1749a135
authored
May 28, 2007
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Explain when groupby() issues a new group.
parent
a0fcb938
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
0 deletions
+8
-0
Doc/lib/libitertools.tex
Doc/lib/libitertools.tex
+8
-0
No files found.
Doc/lib/libitertools.tex
View file @
1749a135
...
...
@@ -138,6 +138,13 @@ by functions or loops that truncate the stream.
identity function and returns the element unchanged. Generally, the
iterable needs to already be sorted on the same key function.
The operation of
\function
{
groupby()
}
is similar to the
\code
{
uniq
}
filter
in
\UNIX
{}
. It generates a break or new group every time the value
of the key function changes (which is why it is usually necessary
to have sorted the data using the same key function). That behavior
differs from SQL's GROUP BY which aggregates common elements regardless
of their input order.
The returned group is itself an iterator that shares the underlying
iterable with
\function
{
groupby()
}
. Because the source is shared, when
the
\function
{
groupby
}
object is advanced, the previous group is no
...
...
@@ -147,6 +154,7 @@ by functions or loops that truncate the stream.
\begin{verbatim}
groups = []
uniquekeys = []
data = sorted(data, key=keyfunc)
for k, g in groupby(data, keyfunc):
groups.append(list(g)) # Store group iterator as a list
uniquekeys.append(k)
...
...
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