[Python-checkins] python/dist/src/Doc/lib libitertools.tex, 1.19,
1.20
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Fri Oct 24 04:45:25 EDT 2003
Update of /cvsroot/python/python/dist/src/Doc/lib
In directory sc8-pr-cvs1:/tmp/cvs-serv26769/Doc/lib
Modified Files:
libitertools.tex
Log Message:
Added itertools.tee()
It works like the pure python verion except:
* it stops storing data after of the iterators gets deallocated
* the data queue is implemented with two stacks instead of one dictionary.
Index: libitertools.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libitertools.tex,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** libitertools.tex 20 Oct 2003 17:01:07 -0000 1.19
--- libitertools.tex 24 Oct 2003 08:45:23 -0000 1.20
***************
*** 109,115 ****
\end{verbatim}
! Note, this is the only member of the toolkit that may require
! significant auxiliary storage (depending on the length of the
! iterable).
\end{funcdesc}
--- 109,114 ----
\end{verbatim}
! Note, this member of the toolkit may require significant
! auxiliary storage (depending on the length of the iterable).
\end{funcdesc}
***************
*** 283,286 ****
--- 282,311 ----
\end{funcdesc}
+ \begin{funcdesc}{tee}{iterable}
+ Return two independent iterators from a single iterable.
+ Equivalent to:
+
+ \begin{verbatim}
+ def tee(iterable):
+ def gen(next, data={}, cnt=[0]):
+ for i in count():
+ if i == cnt[0]:
+ item = data[i] = next()
+ cnt[0] += 1
+ else:
+ item = data.pop(i)
+ yield item
+ it = iter(iterable)
+ return (gen(it.next), gen(it.next))
+ \end{verbatim}
+
+ Note, this member of the toolkit may require significant auxiliary
+ storage (depending on how much temporary data needs to be stored).
+ In general, if one iterator is going use most or all of the data before
+ the other iterator, it is faster to use \function{list()} instead of
+ \function{tee()}.
+ \versionadded{2.4}
+ \end{funcdesc}
+
\subsection{Examples \label{itertools-example}}
***************
*** 370,373 ****
--- 395,409 ----
return sum(imap(operator.mul, vec1, vec2))
+ def flatten(listOfLists):
+ return list(chain(*listOfLists))
+
+ def repeatfunc(func, times=None, *args):
+ "Repeat calls to func with specified arguments."
+ "Example: repeatfunc(random.random)"
+ if times is None:
+ return starmap(func, repeat(args))
+ else:
+ return starmap(func, repeat(args, times))
+
def window(seq, n=2):
"Returns a sliding window (of width n) over data from the iterable"
***************
*** 380,397 ****
result = result[1:] + (elem,)
yield result
-
- def tee(iterable):
- "Return two independent iterators from a single iterable"
- def gen(next, data={}, cnt=[0]):
- dpop = data.pop
- for i in count():
- if i == cnt[0]:
- item = data[i] = next()
- cnt[0] += 1
- else:
- item = dpop(i)
- yield item
- next = iter(iterable).next
- return (gen(next), gen(next))
\end{verbatim}
--- 416,419 ----
More information about the Python-checkins
mailing list