[Python-checkins] python/nondist/sandbox/itertools libitertools.tex,1.13,1.14 test_itertools.py,1.9,1.10
rhettinger@users.sourceforge.net
rhettinger@users.sourceforge.net
Tue, 28 Jan 2003 17:52:06 -0800
Update of /cvsroot/python/python/nondist/sandbox/itertools
In directory sc8-pr-cvs1:/tmp/cvs-serv14741
Modified Files:
libitertools.tex test_itertools.py
Log Message:
More and improved examples
Index: libitertools.tex
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/itertools/libitertools.tex,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** libitertools.tex 28 Jan 2003 02:02:25 -0000 1.13
--- libitertools.tex 29 Jan 2003 01:52:04 -0000 1.14
***************
*** 265,291 ****
>>> import operator
! >>> bases = [2, 3, 5, 7]
! >>> powers = [2, 3, 4]
! >>> for power in powers:
! ... print list(imap(operator.pow, bases, repeat(power)))
...
! [4, 9, 25, 49]
! [8, 27, 125, 343]
! [16, 81, 625, 2401]
\end{verbatim}
As a further example of how itertools can be combined, here
! are a few re-definitions of existing tools:
\begin{verbatim}
! >>> def enumerate(s):
! ... return izip(count(), s)
! >>> def tabulate(f):
! ... return imap(f, count())
! >>> def iteritems(d):
! ... return izip(d.iterkeys(), d.itervalues())
! >>> def nth(s, n):
! ... return islice(n, n+1).next()
\end{verbatim}
--- 265,305 ----
>>> import operator
! >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)):
! ... print cube
...
! 1
! 8
! 27
!
! >>> reportfile = ['EuroPython', 'Roster', '', 'alex', '', 'laura',
! '', 'martin', '', 'walter', '', 'samuele']
! >>> for name in islice(reportfile, 3, len(reportfile), 2):
! ... print name.title()
! ...
! Alex
! Laura
! Martin
! Walter
! Samuele
\end{verbatim}
As a further example of how itertools can be combined, here
! are a few ways to define higher level tools:
\begin{verbatim}
! >>> def enumerate(iterable):
! ... return izip(count(), iterable)
!
! >>> def tabulate(function):
! ... "Return function(0), function(1), ..."
! ... return imap(function, count())
!
! >>> def iteritems(mapping):
! ... return izip(mapping.iterkeys(), mapping.itervalues())
!
! >>> def nth(iterable, n):
! ... "Returns the nth item"
! ... return islice(iterable, n, n+1).next()
\end{verbatim}
Index: test_itertools.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/itertools/test_itertools.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** test_itertools.py 28 Jan 2003 02:02:26 -0000 1.9
--- test_itertools.py 29 Jan 2003 01:52:04 -0000 1.10
***************
*** 100,122 ****
>>> import operator
! >>> bases = [2, 3, 5, 7]
! >>> powers = [2, 3, 4]
! >>> for power in powers:
! ... print list(imap(operator.pow, bases, repeat(power)))
...
! [4, 9, 25, 49]
! [8, 27, 125, 343]
! [16, 81, 625, 2401]
! >>> def enumerate(s):
! ... return izip(count(), s)
! >>> def tabulate(f):
! ... return imap(f, count())
! >>> def iteritems(d):
! ... return izip(d.iterkeys(), d.itervalues())
! >>> def nth(s, n):
! ... return islice(n, n+1).next()
"""
--- 100,134 ----
>>> import operator
! >>> import operator
! >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)):
! ... print cube
...
! 1
! 8
! 27
!
! >>> reportfile = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
! >>> for name in islice(reportfile, 3, len(reportfile), 2):
! ... print name.title()
! ...
! Alex
! Laura
! Martin
! Walter
! Samuele
+ >>> def enumerate(iterable):
+ ... return izip(count(), iterable)
! >>> def tabulate(function):
! ... "Return function(0), function(1), ..."
! ... return imap(function, count())
!
! >>> def iteritems(mapping):
! ... return izip(mapping.iterkeys(), mapping.itervalues())
+ >>> def nth(iterable, n):
+ ... "Returns the nth item"
+ ... return islice(iterable, n, n+1).next()
"""