Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ad80955

Browse files
committed
Add groupby()
1 parent db7dcff commit ad80955

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Doc/whatsnew/whatsnew24.tex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,34 @@ \section{New, Improved, and Deprecated Modules}
266266
\item The \module{imaplib} module now supports IMAP's THREAD command.
267267
(Contributed by Yves Dionne.)
268268

269+
\item The \module{itertools} module gained a
270+
\function{groupby(\var{iterable}\optional{, \var{func}})} function,
271+
inspired by the GROUP BY clause from SQL.
272+
\var{iterable} returns a succession of elements, and the optional
273+
\var{func} is a function that takes an element and returns a key
274+
value; if omitted, the key is simply the element itself.
275+
\function{groupby()} then groups the elements into subsequences
276+
which have matching values of the key, and returns a series of 2-tuples
277+
containing the key value and an iterator over the subsequence.
278+
279+
Here's an example. The \var{key} function simply returns whether a
280+
number is even or odd, so the result of \function{groupby()} is to
281+
return consecutive runs of odd or even numbers.
282+
283+
\begin{verbatim}
284+
>>> import itertools
285+
>>> L = [2,4,6, 7,8,9,11, 12, 14]
286+
>>> for key_val, it in itertools.groupby(L, lambda x: x % 2):
287+
... print key_val, list(it)
288+
...
289+
0 [2, 4, 6]
290+
1 [7]
291+
0 [8]
292+
1 [9, 11]
293+
0 [12, 14]
294+
>>>
295+
\end{verbatim}
296+
269297
\item A new \function{getsid()} function was added to the
270298
\module{posix} module that underlies the \module{os} module.
271299
(Contributed by J. Raynor.)

0 commit comments

Comments
 (0)