2007-08-15 11:28:22 -03:00
|
|
|
|
:mod:`itertools` --- Functions creating iterators for efficient looping
|
|
|
|
|
=======================================================================
|
|
|
|
|
|
|
|
|
|
.. module:: itertools
|
2010-12-01 19:45:20 -04:00
|
|
|
|
:synopsis: Functions creating iterators for efficient looping.
|
2016-06-11 16:02:54 -03:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
.. moduleauthor:: Raymond Hettinger <python@rcn.com>
|
|
|
|
|
.. sectionauthor:: Raymond Hettinger <python@rcn.com>
|
|
|
|
|
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
|
.. testsetup::
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
from itertools import *
|
2022-10-18 16:09:34 -03:00
|
|
|
|
import collections
|
|
|
|
|
import math
|
|
|
|
|
import operator
|
|
|
|
|
import random
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
|
--------------
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
|
|
2009-02-17 16:00:59 -04:00
|
|
|
|
This module implements a number of :term:`iterator` building blocks inspired
|
|
|
|
|
by constructs from APL, Haskell, and SML. Each has been recast in a form
|
|
|
|
|
suitable for Python.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
The module standardizes a core set of fast, memory efficient tools that are
|
2009-02-17 16:00:59 -04:00
|
|
|
|
useful by themselves or in combination. Together, they form an "iterator
|
|
|
|
|
algebra" making it possible to construct specialized tools succinctly and
|
|
|
|
|
efficiently in pure Python.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
For instance, SML provides a tabulation tool: ``tabulate(f)`` which produces a
|
2010-01-21 16:57:24 -04:00
|
|
|
|
sequence ``f(0), f(1), ...``. The same effect can be achieved in Python
|
2008-03-12 22:26:19 -03:00
|
|
|
|
by combining :func:`map` and :func:`count` to form ``map(f, count())``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-03-11 21:29:44 -03:00
|
|
|
|
These tools and their built-in counterparts also work well with the high-speed
|
|
|
|
|
functions in the :mod:`operator` module. For example, the multiplication
|
|
|
|
|
operator can be mapped across two vectors to form an efficient dot-product:
|
2023-01-07 14:46:35 -04:00
|
|
|
|
``sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))``.
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
|
|
|
|
|
2017-12-15 17:17:55 -04:00
|
|
|
|
**Infinite iterators:**
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
2009-04-10 16:02:36 -03:00
|
|
|
|
================== ================= ================================================= =========================================
|
|
|
|
|
Iterator Arguments Results Example
|
|
|
|
|
================== ================= ================================================= =========================================
|
2024-03-14 18:39:50 -03:00
|
|
|
|
:func:`count` [start[, step]] start, start+step, start+2*step, ... ``count(10) → 10 11 12 13 14 ...``
|
|
|
|
|
:func:`cycle` p p0, p1, ... plast, p0, p1, ... ``cycle('ABCD') → A B C D A B C D ...``
|
|
|
|
|
:func:`repeat` elem [,n] elem, elem, elem, ... endlessly or up to n times ``repeat(10, 3) → 10 10 10``
|
2009-04-10 16:02:36 -03:00
|
|
|
|
================== ================= ================================================= =========================================
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
|
|
|
|
**Iterators terminating on the shortest input sequence:**
|
|
|
|
|
|
2014-01-16 11:10:13 -04:00
|
|
|
|
============================ ============================ ================================================= =============================================================
|
|
|
|
|
Iterator Arguments Results Example
|
|
|
|
|
============================ ============================ ================================================= =============================================================
|
2024-03-14 18:39:50 -03:00
|
|
|
|
:func:`accumulate` p [,func] p0, p0+p1, p0+p1+p2, ... ``accumulate([1,2,3,4,5]) → 1 3 6 10 15``
|
|
|
|
|
:func:`batched` p, n (p0, p1, ..., p_n-1), ... ``batched('ABCDEFG', n=3) → ABC DEF G``
|
|
|
|
|
:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') → A B C D E F``
|
|
|
|
|
:func:`chain.from_iterable` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) → A B C D E F``
|
|
|
|
|
:func:`compress` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) → A C E F``
|
|
|
|
|
:func:`dropwhile` predicate, seq seq[n], seq[n+1], starting when predicate fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) → 6 4 1``
|
|
|
|
|
:func:`filterfalse` predicate, seq elements of seq where predicate(elem) fails ``filterfalse(lambda x: x%2, range(10)) → 0 2 4 6 8``
|
2017-09-25 05:21:06 -03:00
|
|
|
|
:func:`groupby` iterable[, key] sub-iterators grouped by value of key(v)
|
2024-03-14 18:39:50 -03:00
|
|
|
|
:func:`islice` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) → C D E F G``
|
|
|
|
|
:func:`pairwise` iterable (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') → AB BC CD DE EF FG``
|
|
|
|
|
:func:`starmap` func, seq func(\*seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) → 32 9 1000``
|
|
|
|
|
:func:`takewhile` predicate, seq seq[0], seq[1], until predicate fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) → 1 4``
|
2014-01-16 11:10:13 -04:00
|
|
|
|
:func:`tee` it, n it1, it2, ... itn splits one iterator into n
|
2024-03-14 18:39:50 -03:00
|
|
|
|
:func:`zip_longest` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') → Ax By C- D-``
|
2014-01-16 11:10:13 -04:00
|
|
|
|
============================ ============================ ================================================= =============================================================
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
2017-12-15 17:17:55 -04:00
|
|
|
|
**Combinatoric iterators:**
|
2009-02-17 16:00:59 -04:00
|
|
|
|
|
2009-04-10 16:43:50 -03:00
|
|
|
|
============================================== ==================== =============================================================
|
|
|
|
|
Iterator Arguments Results
|
|
|
|
|
============================================== ==================== =============================================================
|
|
|
|
|
:func:`product` p, q, ... [repeat=1] cartesian product, equivalent to a nested for-loop
|
|
|
|
|
:func:`permutations` p[, r] r-length tuples, all possible orderings, no repeated elements
|
2009-11-18 21:20:07 -04:00
|
|
|
|
:func:`combinations` p, r r-length tuples, in sorted order, no repeated elements
|
|
|
|
|
:func:`combinations_with_replacement` p, r r-length tuples, in sorted order, with repeated elements
|
2009-04-10 16:43:50 -03:00
|
|
|
|
============================================== ==================== =============================================================
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2019-03-21 12:28:49 -03:00
|
|
|
|
============================================== =============================================================
|
|
|
|
|
Examples Results
|
|
|
|
|
============================================== =============================================================
|
|
|
|
|
``product('ABCD', repeat=2)`` ``AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD``
|
|
|
|
|
``permutations('ABCD', 2)`` ``AB AC AD BA BC BD CA CB CD DA DB DC``
|
|
|
|
|
``combinations('ABCD', 2)`` ``AB AC AD BC BD CD``
|
2024-03-14 18:39:50 -03:00
|
|
|
|
``combinations_with_replacement('ABCD', 2)`` ``AA AB AC AD BB BC BD CC CD DD``
|
2019-03-21 12:28:49 -03:00
|
|
|
|
============================================== =============================================================
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. _itertools-functions:
|
|
|
|
|
|
2024-03-04 11:51:29 -04:00
|
|
|
|
Itertool Functions
|
2007-08-15 11:28:22 -03:00
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
The following module functions all construct and return iterators. Some provide
|
|
|
|
|
streams of infinite length, so they should only be accessed by functions or
|
|
|
|
|
loops that truncate the stream.
|
|
|
|
|
|
2018-09-23 21:34:59 -03:00
|
|
|
|
.. function:: accumulate(iterable[, func, *, initial=None])
|
2010-12-01 18:50:36 -04:00
|
|
|
|
|
2014-04-15 23:28:40 -03:00
|
|
|
|
Make an iterator that returns accumulated sums, or accumulated
|
|
|
|
|
results of other binary functions (specified via the optional
|
2018-09-23 21:34:59 -03:00
|
|
|
|
*func* argument).
|
|
|
|
|
|
|
|
|
|
If *func* is supplied, it should be a function
|
2014-04-15 23:28:40 -03:00
|
|
|
|
of two arguments. Elements of the input *iterable* may be any type
|
|
|
|
|
that can be accepted as arguments to *func*. (For example, with
|
|
|
|
|
the default operation of addition, elements may be any addable
|
|
|
|
|
type including :class:`~decimal.Decimal` or
|
2018-09-23 21:34:59 -03:00
|
|
|
|
:class:`~fractions.Fraction`.)
|
|
|
|
|
|
|
|
|
|
Usually, the number of elements output matches the input iterable.
|
|
|
|
|
However, if the keyword argument *initial* is provided, the
|
|
|
|
|
accumulation leads off with the *initial* value so that the output
|
|
|
|
|
has one more element than the input iterable.
|
2010-12-01 18:50:36 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
2011-03-27 22:52:10 -03:00
|
|
|
|
|
2018-09-23 21:34:59 -03:00
|
|
|
|
def accumulate(iterable, func=operator.add, *, initial=None):
|
2010-12-01 18:50:36 -04:00
|
|
|
|
'Return running totals'
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# accumulate([1,2,3,4,5]) → 1 3 6 10 15
|
|
|
|
|
# accumulate([1,2,3,4,5], initial=100) → 100 101 103 106 110 115
|
|
|
|
|
# accumulate([1,2,3,4,5], operator.mul) → 1 2 6 24 120
|
2010-12-02 22:09:34 -04:00
|
|
|
|
it = iter(iterable)
|
2018-09-23 21:34:59 -03:00
|
|
|
|
total = initial
|
|
|
|
|
if initial is None:
|
|
|
|
|
try:
|
|
|
|
|
total = next(it)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
return
|
2010-12-02 22:09:34 -04:00
|
|
|
|
yield total
|
|
|
|
|
for element in it:
|
2011-03-27 22:52:10 -03:00
|
|
|
|
total = func(total, element)
|
2010-12-02 22:09:34 -04:00
|
|
|
|
yield total
|
2010-12-01 18:50:36 -04:00
|
|
|
|
|
2011-04-21 15:09:28 -03:00
|
|
|
|
There are a number of uses for the *func* argument. It can be set to
|
|
|
|
|
:func:`min` for a running minimum, :func:`max` for a running maximum, or
|
|
|
|
|
:func:`operator.mul` for a running product. Amortization tables can be
|
2022-10-18 16:09:34 -03:00
|
|
|
|
built by accumulating interest and applying payments:
|
|
|
|
|
|
|
|
|
|
.. doctest::
|
2011-03-27 22:52:10 -03:00
|
|
|
|
|
|
|
|
|
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
|
|
|
|
|
>>> list(accumulate(data, operator.mul)) # running product
|
|
|
|
|
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
|
|
|
|
|
>>> list(accumulate(data, max)) # running maximum
|
|
|
|
|
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
|
|
|
|
|
|
2023-05-25 00:31:02 -03:00
|
|
|
|
# Amortize a 5% loan of 1000 with 10 annual payments of 90
|
|
|
|
|
>>> account_update = lambda bal, pmt: round(bal * 1.05) + pmt
|
|
|
|
|
>>> list(accumulate(repeat(-90, 10), account_update, initial=1_000))
|
|
|
|
|
[1000, 960, 918, 874, 828, 779, 728, 674, 618, 559, 497]
|
2011-03-27 22:52:10 -03:00
|
|
|
|
|
2013-10-12 20:04:17 -03:00
|
|
|
|
See :func:`functools.reduce` for a similar function that returns only the
|
|
|
|
|
final accumulated value.
|
|
|
|
|
|
2010-12-01 18:50:36 -04:00
|
|
|
|
.. versionadded:: 3.2
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2011-03-27 22:52:10 -03:00
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
|
Added the optional *func* parameter.
|
|
|
|
|
|
2018-09-23 21:34:59 -03:00
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
Added the optional *initial* parameter.
|
|
|
|
|
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
2023-12-16 11:13:50 -04:00
|
|
|
|
.. function:: batched(iterable, n, *, strict=False)
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
2022-12-08 17:08:16 -04:00
|
|
|
|
Batch data from the *iterable* into tuples of length *n*. The last
|
2022-10-17 20:53:45 -03:00
|
|
|
|
batch may be shorter than *n*.
|
|
|
|
|
|
2023-12-16 11:13:50 -04:00
|
|
|
|
If *strict* is true, will raise a :exc:`ValueError` if the final
|
|
|
|
|
batch is shorter than *n*.
|
|
|
|
|
|
2022-12-08 17:08:16 -04:00
|
|
|
|
Loops over the input iterable and accumulates data into tuples up to
|
|
|
|
|
size *n*. The input is consumed lazily, just enough to fill a batch.
|
2022-10-17 20:53:45 -03:00
|
|
|
|
The result is yielded as soon as the batch is full or when the input
|
|
|
|
|
iterable is exhausted:
|
|
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
|
|
>>> flattened_data = ['roses', 'red', 'violets', 'blue', 'sugar', 'sweet']
|
|
|
|
|
>>> unflattened = list(batched(flattened_data, 2))
|
|
|
|
|
>>> unflattened
|
2022-12-08 17:08:16 -04:00
|
|
|
|
[('roses', 'red'), ('violets', 'blue'), ('sugar', 'sweet')]
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
|
|
|
|
>>> for batch in batched('ABCDEFG', 3):
|
|
|
|
|
... print(batch)
|
|
|
|
|
...
|
2022-12-08 17:08:16 -04:00
|
|
|
|
('A', 'B', 'C')
|
|
|
|
|
('D', 'E', 'F')
|
|
|
|
|
('G',)
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
|
|
|
|
Roughly equivalent to::
|
|
|
|
|
|
2023-12-16 11:13:50 -04:00
|
|
|
|
def batched(iterable, n, *, strict=False):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# batched('ABCDEFG', 3) → ABC DEF G
|
2022-10-17 20:53:45 -03:00
|
|
|
|
if n < 1:
|
|
|
|
|
raise ValueError('n must be at least one')
|
|
|
|
|
it = iter(iterable)
|
2023-03-21 14:06:18 -03:00
|
|
|
|
while batch := tuple(islice(it, n)):
|
2023-12-16 11:13:50 -04:00
|
|
|
|
if strict and len(batch) != n:
|
|
|
|
|
raise ValueError('batched(): incomplete batch')
|
2022-10-17 20:53:45 -03:00
|
|
|
|
yield batch
|
|
|
|
|
|
2022-10-18 22:14:41 -03:00
|
|
|
|
.. versionadded:: 3.12
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
2023-12-16 11:13:50 -04:00
|
|
|
|
.. versionchanged:: 3.13
|
|
|
|
|
Added the *strict* option.
|
|
|
|
|
|
2022-10-17 20:53:45 -03:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
.. function:: chain(*iterables)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns elements from the first iterable until it is
|
|
|
|
|
exhausted, then proceeds to the next iterable, until all of the iterables are
|
|
|
|
|
exhausted. Used for treating consecutive sequences as a single sequence.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def chain(*iterables):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# chain('ABC', 'DEF') → A B C D E F
|
2010-12-01 19:45:20 -04:00
|
|
|
|
for it in iterables:
|
|
|
|
|
for element in it:
|
|
|
|
|
yield element
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
2010-07-29 11:36:11 -03:00
|
|
|
|
.. classmethod:: chain.from_iterable(iterable)
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Alternate constructor for :func:`chain`. Gets chained inputs from a
|
2013-09-09 03:54:27 -03:00
|
|
|
|
single iterable argument that is evaluated lazily. Roughly equivalent to::
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def from_iterable(iterables):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# chain.from_iterable(['ABC', 'DEF']) → A B C D E F
|
2010-12-01 19:45:20 -04:00
|
|
|
|
for it in iterables:
|
|
|
|
|
for element in it:
|
|
|
|
|
yield element
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61209 | georg.brandl | 2008-03-03 21:37:55 +0100 (Mon, 03 Mar 2008) | 2 lines
There are now sixteen isfoo functions.
........
r61210 | georg.brandl | 2008-03-03 21:39:00 +0100 (Mon, 03 Mar 2008) | 2 lines
15 -> 16, the 2nd
........
r61211 | georg.brandl | 2008-03-03 22:22:47 +0100 (Mon, 03 Mar 2008) | 2 lines
Actually import itertools.
........
r61212 | georg.brandl | 2008-03-03 22:31:50 +0100 (Mon, 03 Mar 2008) | 2 lines
Expand a bit on genexp scopes.
........
r61213 | raymond.hettinger | 2008-03-03 23:04:55 +0100 (Mon, 03 Mar 2008) | 1 line
Remove dependency on itertools -- a simple genexp suffices.
........
r61214 | raymond.hettinger | 2008-03-03 23:19:58 +0100 (Mon, 03 Mar 2008) | 1 line
Issue 2226: Callable checked for the wrong abstract method.
........
r61217 | andrew.kuchling | 2008-03-04 01:40:32 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61218 | andrew.kuchling | 2008-03-04 02:30:10 +0100 (Tue, 04 Mar 2008) | 1 line
Grammar fix; markup fix
........
r61219 | andrew.kuchling | 2008-03-04 02:47:38 +0100 (Tue, 04 Mar 2008) | 1 line
Fix sentence fragment
........
r61220 | andrew.kuchling | 2008-03-04 02:48:26 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61221 | andrew.kuchling | 2008-03-04 02:49:37 +0100 (Tue, 04 Mar 2008) | 1 line
Add versionadded tags
........
r61222 | andrew.kuchling | 2008-03-04 02:50:32 +0100 (Tue, 04 Mar 2008) | 1 line
Thesis night results: add various items
........
r61224 | raymond.hettinger | 2008-03-04 05:17:08 +0100 (Tue, 04 Mar 2008) | 1 line
Beef-up docs and tests for itertools. Fix-up end-case for product().
........
r61225 | georg.brandl | 2008-03-04 08:25:54 +0100 (Tue, 04 Mar 2008) | 2 lines
Fix some patch attributions.
........
r61226 | georg.brandl | 2008-03-04 08:33:30 +0100 (Tue, 04 Mar 2008) | 2 lines
#2230: document that PyArg_* leaves addresses alone on error.
........
r61233 | neal.norwitz | 2008-03-04 17:22:46 +0100 (Tue, 04 Mar 2008) | 3 lines
Close the file before trying to remove the directory so it works on Windows.
As reported by Trent Nelson on python-dev.
........
r61234 | thomas.heller | 2008-03-04 21:09:11 +0100 (Tue, 04 Mar 2008) | 9 lines
Merged changes from libffi3-branch.
The bundled libffi copy is now in sync with the recently released
libffi3.0.4 version, apart from some small changes to
Modules/_ctypes/libffi/configure.ac.
I gave up on using libffi3 files on os x.
Instead, static configuration with files from pyobjc is used.
........
r61235 | thomas.heller | 2008-03-04 21:21:42 +0100 (Tue, 04 Mar 2008) | 1 line
Try to fix the build for PY_LINUX.
........
r61236 | fred.drake | 2008-03-04 22:14:04 +0100 (Tue, 04 Mar 2008) | 2 lines
fix typo
........
r61237 | raymond.hettinger | 2008-03-04 23:29:44 +0100 (Tue, 04 Mar 2008) | 1 line
Fix refleak in chain().
........
2008-03-04 19:39:23 -04:00
|
|
|
|
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
.. function:: combinations(iterable, r)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Return *r* length subsequences of elements from the input *iterable*.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2020-05-28 16:56:43 -03:00
|
|
|
|
The combination tuples are emitted in lexicographic ordering according to
|
|
|
|
|
the order of the input *iterable*. So, if the input *iterable* is sorted,
|
2022-10-18 16:09:34 -03:00
|
|
|
|
the output tuples will be produced in sorted order.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Elements are treated as unique based on their position, not on their
|
2022-10-18 16:09:34 -03:00
|
|
|
|
value. So if the input elements are unique, there will be no repeated
|
2010-12-01 19:45:20 -04:00
|
|
|
|
values in each combination.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
|
|
|
|
def combinations(iterable, r):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# combinations('ABCD', 2) → AB AC AD BC BD CD
|
|
|
|
|
# combinations(range(4), 3) → 012 013 023 123
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
pool = tuple(iterable)
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61081 | neal.norwitz | 2008-02-26 09:04:59 +0100 (Tue, 26 Feb 2008) | 7 lines
Speed up this test by about 99%. Remove sleeps and replace with events.
(This may fail on some slow platforms, but we can fix those cases which
should be relatively isolated and easier to find now.)
Move two test cases that didn't require a server to be started
to a separate TestCase. These tests were taking 3 seconds which
is what the timeout was set to.
........
r61082 | christian.heimes | 2008-02-26 09:18:11 +0100 (Tue, 26 Feb 2008) | 1 line
The contains function raised a gcc warning. The new code is copied straight from py3k.
........
r61084 | neal.norwitz | 2008-02-26 09:21:28 +0100 (Tue, 26 Feb 2008) | 3 lines
Add a timing flag to Trace so you can see where slowness occurs
like waiting for socket timeouts in test_smtplib :-).
........
r61086 | christian.heimes | 2008-02-26 18:23:51 +0100 (Tue, 26 Feb 2008) | 3 lines
Patch #1691070 from Roger Upole: Speed up PyArg_ParseTupleAndKeywords() and improve error msg
My tests don't show the promised speed up of 10%. The code is as fast as the old code for simple cases and slightly faster for complex cases with several of args and kwargs. But the patch simplifies the code, too.
........
r61087 | georg.brandl | 2008-02-26 20:13:45 +0100 (Tue, 26 Feb 2008) | 2 lines
#2194: fix some typos.
........
r61088 | raymond.hettinger | 2008-02-27 00:40:50 +0100 (Wed, 27 Feb 2008) | 1 line
Add itertools.combinations().
........
r61089 | raymond.hettinger | 2008-02-27 02:08:04 +0100 (Wed, 27 Feb 2008) | 1 line
One too many decrefs.
........
r61090 | raymond.hettinger | 2008-02-27 02:08:30 +0100 (Wed, 27 Feb 2008) | 1 line
Larger test range
........
r61091 | raymond.hettinger | 2008-02-27 02:44:34 +0100 (Wed, 27 Feb 2008) | 1 line
Simply the sample code for combinations().
........
2008-02-28 07:19:05 -04:00
|
|
|
|
n = len(pool)
|
2009-01-08 17:01:54 -04:00
|
|
|
|
if r > n:
|
|
|
|
|
return
|
|
|
|
|
indices = list(range(r))
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
yield tuple(pool[i] for i in indices)
|
2009-02-18 16:56:51 -04:00
|
|
|
|
while True:
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61081 | neal.norwitz | 2008-02-26 09:04:59 +0100 (Tue, 26 Feb 2008) | 7 lines
Speed up this test by about 99%. Remove sleeps and replace with events.
(This may fail on some slow platforms, but we can fix those cases which
should be relatively isolated and easier to find now.)
Move two test cases that didn't require a server to be started
to a separate TestCase. These tests were taking 3 seconds which
is what the timeout was set to.
........
r61082 | christian.heimes | 2008-02-26 09:18:11 +0100 (Tue, 26 Feb 2008) | 1 line
The contains function raised a gcc warning. The new code is copied straight from py3k.
........
r61084 | neal.norwitz | 2008-02-26 09:21:28 +0100 (Tue, 26 Feb 2008) | 3 lines
Add a timing flag to Trace so you can see where slowness occurs
like waiting for socket timeouts in test_smtplib :-).
........
r61086 | christian.heimes | 2008-02-26 18:23:51 +0100 (Tue, 26 Feb 2008) | 3 lines
Patch #1691070 from Roger Upole: Speed up PyArg_ParseTupleAndKeywords() and improve error msg
My tests don't show the promised speed up of 10%. The code is as fast as the old code for simple cases and slightly faster for complex cases with several of args and kwargs. But the patch simplifies the code, too.
........
r61087 | georg.brandl | 2008-02-26 20:13:45 +0100 (Tue, 26 Feb 2008) | 2 lines
#2194: fix some typos.
........
r61088 | raymond.hettinger | 2008-02-27 00:40:50 +0100 (Wed, 27 Feb 2008) | 1 line
Add itertools.combinations().
........
r61089 | raymond.hettinger | 2008-02-27 02:08:04 +0100 (Wed, 27 Feb 2008) | 1 line
One too many decrefs.
........
r61090 | raymond.hettinger | 2008-02-27 02:08:30 +0100 (Wed, 27 Feb 2008) | 1 line
Larger test range
........
r61091 | raymond.hettinger | 2008-02-27 02:44:34 +0100 (Wed, 27 Feb 2008) | 1 line
Simply the sample code for combinations().
........
2008-02-28 07:19:05 -04:00
|
|
|
|
for i in reversed(range(r)):
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
if indices[i] != i + n - r:
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
break
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61081 | neal.norwitz | 2008-02-26 09:04:59 +0100 (Tue, 26 Feb 2008) | 7 lines
Speed up this test by about 99%. Remove sleeps and replace with events.
(This may fail on some slow platforms, but we can fix those cases which
should be relatively isolated and easier to find now.)
Move two test cases that didn't require a server to be started
to a separate TestCase. These tests were taking 3 seconds which
is what the timeout was set to.
........
r61082 | christian.heimes | 2008-02-26 09:18:11 +0100 (Tue, 26 Feb 2008) | 1 line
The contains function raised a gcc warning. The new code is copied straight from py3k.
........
r61084 | neal.norwitz | 2008-02-26 09:21:28 +0100 (Tue, 26 Feb 2008) | 3 lines
Add a timing flag to Trace so you can see where slowness occurs
like waiting for socket timeouts in test_smtplib :-).
........
r61086 | christian.heimes | 2008-02-26 18:23:51 +0100 (Tue, 26 Feb 2008) | 3 lines
Patch #1691070 from Roger Upole: Speed up PyArg_ParseTupleAndKeywords() and improve error msg
My tests don't show the promised speed up of 10%. The code is as fast as the old code for simple cases and slightly faster for complex cases with several of args and kwargs. But the patch simplifies the code, too.
........
r61087 | georg.brandl | 2008-02-26 20:13:45 +0100 (Tue, 26 Feb 2008) | 2 lines
#2194: fix some typos.
........
r61088 | raymond.hettinger | 2008-02-27 00:40:50 +0100 (Wed, 27 Feb 2008) | 1 line
Add itertools.combinations().
........
r61089 | raymond.hettinger | 2008-02-27 02:08:04 +0100 (Wed, 27 Feb 2008) | 1 line
One too many decrefs.
........
r61090 | raymond.hettinger | 2008-02-27 02:08:30 +0100 (Wed, 27 Feb 2008) | 1 line
Larger test range
........
r61091 | raymond.hettinger | 2008-02-27 02:44:34 +0100 (Wed, 27 Feb 2008) | 1 line
Simply the sample code for combinations().
........
2008-02-28 07:19:05 -04:00
|
|
|
|
else:
|
|
|
|
|
return
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
indices[i] += 1
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61081-61095 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61081 | neal.norwitz | 2008-02-26 09:04:59 +0100 (Tue, 26 Feb 2008) | 7 lines
Speed up this test by about 99%. Remove sleeps and replace with events.
(This may fail on some slow platforms, but we can fix those cases which
should be relatively isolated and easier to find now.)
Move two test cases that didn't require a server to be started
to a separate TestCase. These tests were taking 3 seconds which
is what the timeout was set to.
........
r61082 | christian.heimes | 2008-02-26 09:18:11 +0100 (Tue, 26 Feb 2008) | 1 line
The contains function raised a gcc warning. The new code is copied straight from py3k.
........
r61084 | neal.norwitz | 2008-02-26 09:21:28 +0100 (Tue, 26 Feb 2008) | 3 lines
Add a timing flag to Trace so you can see where slowness occurs
like waiting for socket timeouts in test_smtplib :-).
........
r61086 | christian.heimes | 2008-02-26 18:23:51 +0100 (Tue, 26 Feb 2008) | 3 lines
Patch #1691070 from Roger Upole: Speed up PyArg_ParseTupleAndKeywords() and improve error msg
My tests don't show the promised speed up of 10%. The code is as fast as the old code for simple cases and slightly faster for complex cases with several of args and kwargs. But the patch simplifies the code, too.
........
r61087 | georg.brandl | 2008-02-26 20:13:45 +0100 (Tue, 26 Feb 2008) | 2 lines
#2194: fix some typos.
........
r61088 | raymond.hettinger | 2008-02-27 00:40:50 +0100 (Wed, 27 Feb 2008) | 1 line
Add itertools.combinations().
........
r61089 | raymond.hettinger | 2008-02-27 02:08:04 +0100 (Wed, 27 Feb 2008) | 1 line
One too many decrefs.
........
r61090 | raymond.hettinger | 2008-02-27 02:08:30 +0100 (Wed, 27 Feb 2008) | 1 line
Larger test range
........
r61091 | raymond.hettinger | 2008-02-27 02:44:34 +0100 (Wed, 27 Feb 2008) | 1 line
Simply the sample code for combinations().
........
2008-02-28 07:19:05 -04:00
|
|
|
|
for j in range(i+1, r):
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
indices[j] = indices[j-1] + 1
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The code for :func:`combinations` can be also expressed as a subsequence
|
|
|
|
|
of :func:`permutations` after filtering entries where the elements are not
|
|
|
|
|
in sorted order (according to their position in the input pool)::
|
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61209 | georg.brandl | 2008-03-03 21:37:55 +0100 (Mon, 03 Mar 2008) | 2 lines
There are now sixteen isfoo functions.
........
r61210 | georg.brandl | 2008-03-03 21:39:00 +0100 (Mon, 03 Mar 2008) | 2 lines
15 -> 16, the 2nd
........
r61211 | georg.brandl | 2008-03-03 22:22:47 +0100 (Mon, 03 Mar 2008) | 2 lines
Actually import itertools.
........
r61212 | georg.brandl | 2008-03-03 22:31:50 +0100 (Mon, 03 Mar 2008) | 2 lines
Expand a bit on genexp scopes.
........
r61213 | raymond.hettinger | 2008-03-03 23:04:55 +0100 (Mon, 03 Mar 2008) | 1 line
Remove dependency on itertools -- a simple genexp suffices.
........
r61214 | raymond.hettinger | 2008-03-03 23:19:58 +0100 (Mon, 03 Mar 2008) | 1 line
Issue 2226: Callable checked for the wrong abstract method.
........
r61217 | andrew.kuchling | 2008-03-04 01:40:32 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61218 | andrew.kuchling | 2008-03-04 02:30:10 +0100 (Tue, 04 Mar 2008) | 1 line
Grammar fix; markup fix
........
r61219 | andrew.kuchling | 2008-03-04 02:47:38 +0100 (Tue, 04 Mar 2008) | 1 line
Fix sentence fragment
........
r61220 | andrew.kuchling | 2008-03-04 02:48:26 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61221 | andrew.kuchling | 2008-03-04 02:49:37 +0100 (Tue, 04 Mar 2008) | 1 line
Add versionadded tags
........
r61222 | andrew.kuchling | 2008-03-04 02:50:32 +0100 (Tue, 04 Mar 2008) | 1 line
Thesis night results: add various items
........
r61224 | raymond.hettinger | 2008-03-04 05:17:08 +0100 (Tue, 04 Mar 2008) | 1 line
Beef-up docs and tests for itertools. Fix-up end-case for product().
........
r61225 | georg.brandl | 2008-03-04 08:25:54 +0100 (Tue, 04 Mar 2008) | 2 lines
Fix some patch attributions.
........
r61226 | georg.brandl | 2008-03-04 08:33:30 +0100 (Tue, 04 Mar 2008) | 2 lines
#2230: document that PyArg_* leaves addresses alone on error.
........
r61233 | neal.norwitz | 2008-03-04 17:22:46 +0100 (Tue, 04 Mar 2008) | 3 lines
Close the file before trying to remove the directory so it works on Windows.
As reported by Trent Nelson on python-dev.
........
r61234 | thomas.heller | 2008-03-04 21:09:11 +0100 (Tue, 04 Mar 2008) | 9 lines
Merged changes from libffi3-branch.
The bundled libffi copy is now in sync with the recently released
libffi3.0.4 version, apart from some small changes to
Modules/_ctypes/libffi/configure.ac.
I gave up on using libffi3 files on os x.
Instead, static configuration with files from pyobjc is used.
........
r61235 | thomas.heller | 2008-03-04 21:21:42 +0100 (Tue, 04 Mar 2008) | 1 line
Try to fix the build for PY_LINUX.
........
r61236 | fred.drake | 2008-03-04 22:14:04 +0100 (Tue, 04 Mar 2008) | 2 lines
fix typo
........
r61237 | raymond.hettinger | 2008-03-04 23:29:44 +0100 (Tue, 04 Mar 2008) | 1 line
Fix refleak in chain().
........
2008-03-04 19:39:23 -04:00
|
|
|
|
|
|
|
|
|
def combinations(iterable, r):
|
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
for indices in permutations(range(n), r):
|
|
|
|
|
if sorted(indices) == list(indices):
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n``
|
|
|
|
|
or zero when ``r > n``.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61064,61066-61080 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61063 | andrew.kuchling | 2008-02-25 17:29:19 +0100 (Mon, 25 Feb 2008) | 1 line
Move .setupterm() output so that we don't try to call endwin() if it fails
........
r61064 | andrew.kuchling | 2008-02-25 17:29:58 +0100 (Mon, 25 Feb 2008) | 1 line
Use file descriptor for real stdout
........
r61067 | facundo.batista | 2008-02-25 19:06:00 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2117. Update compiler module to handle class decorators.
Thanks Thomas Herve
........
r61069 | georg.brandl | 2008-02-25 21:17:56 +0100 (Mon, 25 Feb 2008) | 2 lines
Rename sphinx.addons to sphinx.ext.
........
r61071 | georg.brandl | 2008-02-25 21:20:45 +0100 (Mon, 25 Feb 2008) | 2 lines
Revert r61029.
........
r61072 | facundo.batista | 2008-02-25 23:33:55 +0100 (Mon, 25 Feb 2008) | 4 lines
Issue 2168. gdbm and dbm needs to be iterable; this fixes a
failure in the shelve module. Thanks Thomas Herve.
........
r61073 | raymond.hettinger | 2008-02-25 23:42:32 +0100 (Mon, 25 Feb 2008) | 1 line
Make sure the itertools filter functions give the same performance for func=bool as func=None.
........
r61074 | raymond.hettinger | 2008-02-26 00:17:41 +0100 (Tue, 26 Feb 2008) | 1 line
Revert part of r60927 which made invalid assumptions about the API offered by db modules.
........
r61075 | facundo.batista | 2008-02-26 00:46:02 +0100 (Tue, 26 Feb 2008) | 3 lines
Coerced PyBool_Type to be able to compare it.
........
r61076 | raymond.hettinger | 2008-02-26 03:46:54 +0100 (Tue, 26 Feb 2008) | 1 line
Docs for itertools.combinations(). Implementation in forthcoming checkin.
........
r61077 | neal.norwitz | 2008-02-26 05:50:37 +0100 (Tue, 26 Feb 2008) | 3 lines
Don't use a hard coded port. This test could hang/fail if the port is in use.
Speed this test up by avoiding a sleep and using the event.
........
r61078 | neal.norwitz | 2008-02-26 06:12:50 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61079 | neal.norwitz | 2008-02-26 06:23:51 +0100 (Tue, 26 Feb 2008) | 1 line
Whitespace normalization
........
r61080 | georg.brandl | 2008-02-26 07:40:10 +0100 (Tue, 26 Feb 2008) | 2 lines
Banish tab.
........
2008-02-26 04:18:30 -04:00
|
|
|
|
|
2009-01-27 00:20:44 -04:00
|
|
|
|
.. function:: combinations_with_replacement(iterable, r)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Return *r* length subsequences of elements from the input *iterable*
|
|
|
|
|
allowing individual elements to be repeated more than once.
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2020-05-28 16:56:43 -03:00
|
|
|
|
The combination tuples are emitted in lexicographic ordering according to
|
|
|
|
|
the order of the input *iterable*. So, if the input *iterable* is sorted,
|
2022-10-18 16:09:34 -03:00
|
|
|
|
the output tuples will be produced in sorted order.
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Elements are treated as unique based on their position, not on their
|
|
|
|
|
value. So if the input elements are unique, the generated combinations
|
|
|
|
|
will also be unique.
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
|
|
|
|
def combinations_with_replacement(iterable, r):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# combinations_with_replacement('ABC', 2) → AA AB AC BB BC CC
|
2009-01-27 00:20:44 -04:00
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
if not n and r:
|
|
|
|
|
return
|
|
|
|
|
indices = [0] * r
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
2009-02-18 16:56:51 -04:00
|
|
|
|
while True:
|
2009-01-27 00:20:44 -04:00
|
|
|
|
for i in reversed(range(r)):
|
|
|
|
|
if indices[i] != n - 1:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
return
|
|
|
|
|
indices[i:] = [indices[i] + 1] * (r - i)
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The code for :func:`combinations_with_replacement` can be also expressed as
|
|
|
|
|
a subsequence of :func:`product` after filtering entries where the elements
|
|
|
|
|
are not in sorted order (according to their position in the input pool)::
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
|
|
|
|
def combinations_with_replacement(iterable, r):
|
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
for indices in product(range(n), repeat=r):
|
|
|
|
|
if sorted(indices) == list(indices):
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The number of items returned is ``(n+r-1)! / r! / (n-1)!`` when ``n > 0``.
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
.. versionadded:: 3.1
|
2009-01-27 00:20:44 -04:00
|
|
|
|
|
2010-08-17 12:07:14 -03:00
|
|
|
|
|
2009-01-25 22:56:58 -04:00
|
|
|
|
.. function:: compress(data, selectors)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that filters elements from *data* returning only those that
|
|
|
|
|
have a corresponding element in *selectors* that evaluates to ``True``.
|
|
|
|
|
Stops when either the *data* or *selectors* iterables has been exhausted.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
2009-01-25 22:56:58 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def compress(data, selectors):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# compress('ABCDEF', [1,0,1,0,1,1]) → A C E F
|
2010-12-01 19:45:20 -04:00
|
|
|
|
return (d for d, s in zip(data, selectors) if s)
|
2009-01-25 22:56:58 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
.. versionadded:: 3.1
|
2009-01-25 22:56:58 -04:00
|
|
|
|
|
|
|
|
|
|
2009-02-14 00:21:49 -04:00
|
|
|
|
.. function:: count(start=0, step=1)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2013-06-21 09:00:58 -03:00
|
|
|
|
Make an iterator that returns evenly spaced values starting with number *start*. Often
|
2010-12-01 19:45:20 -04:00
|
|
|
|
used as an argument to :func:`map` to generate consecutive data points.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Also, used with :func:`zip` to add sequence numbers. Roughly equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def count(start=0, step=1):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# count(10) → 10 11 12 13 14 ...
|
|
|
|
|
# count(2.5, 0.5) → 2.5 3.0 3.5 ...
|
2010-12-01 19:45:20 -04:00
|
|
|
|
n = start
|
|
|
|
|
while True:
|
|
|
|
|
yield n
|
|
|
|
|
n += step
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
When counting with floating point numbers, better accuracy can sometimes be
|
|
|
|
|
achieved by substituting multiplicative code such as: ``(start + step * i
|
|
|
|
|
for i in count())``.
|
2009-06-16 22:40:52 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
.. versionchanged:: 3.1
|
|
|
|
|
Added *step* argument and allowed non-integer arguments.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. function:: cycle(iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator returning elements from the iterable and saving a copy of each.
|
|
|
|
|
When the iterable is exhausted, return elements from the saved copy. Repeats
|
2016-05-28 04:17:54 -03:00
|
|
|
|
indefinitely. Roughly equivalent to::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
def cycle(iterable):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# cycle('ABCD') → A B C D A B C D A B C D ...
|
2010-12-01 19:45:20 -04:00
|
|
|
|
saved = []
|
|
|
|
|
for element in iterable:
|
|
|
|
|
yield element
|
|
|
|
|
saved.append(element)
|
|
|
|
|
while saved:
|
|
|
|
|
for element in saved:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
yield element
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Note, this member of the toolkit may require significant auxiliary storage
|
|
|
|
|
(depending on the length of the iterable).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: dropwhile(predicate, iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that drops elements from the iterable as long as the predicate
|
|
|
|
|
is true; afterwards, returns every element. Note, the iterator does not produce
|
|
|
|
|
*any* output until the predicate first becomes false, so it may have a lengthy
|
2016-05-28 04:17:54 -03:00
|
|
|
|
start-up time. Roughly equivalent to::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
def dropwhile(predicate, iterable):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# dropwhile(lambda x: x<5, [1,4,6,4,1]) → 6 4 1
|
2010-12-01 19:45:20 -04:00
|
|
|
|
iterable = iter(iterable)
|
|
|
|
|
for x in iterable:
|
|
|
|
|
if not predicate(x):
|
|
|
|
|
yield x
|
|
|
|
|
break
|
|
|
|
|
for x in iterable:
|
|
|
|
|
yield x
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-01-27 00:42:48 -04:00
|
|
|
|
.. function:: filterfalse(predicate, iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that filters elements from iterable returning only those for
|
2023-02-24 12:13:05 -04:00
|
|
|
|
which the predicate is false. If *predicate* is ``None``, return the items
|
2016-05-28 04:17:54 -03:00
|
|
|
|
that are false. Roughly equivalent to::
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def filterfalse(predicate, iterable):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# filterfalse(lambda x: x%2, range(10)) → 0 2 4 6 8
|
2010-12-01 19:45:20 -04:00
|
|
|
|
if predicate is None:
|
|
|
|
|
predicate = bool
|
|
|
|
|
for x in iterable:
|
|
|
|
|
if not predicate(x):
|
|
|
|
|
yield x
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: groupby(iterable, key=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns consecutive keys and groups from the *iterable*.
|
|
|
|
|
The *key* is a function computing a key value for each element. If not
|
|
|
|
|
specified or is ``None``, *key* defaults to an identity function and returns
|
|
|
|
|
the element unchanged. Generally, the iterable needs to already be sorted on
|
|
|
|
|
the same key function.
|
|
|
|
|
|
|
|
|
|
The operation of :func:`groupby` is similar to the ``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 :func:`groupby`. Because the source is shared, when the :func:`groupby`
|
|
|
|
|
object is advanced, the previous group is no longer visible. So, if that data
|
|
|
|
|
is needed later, it should be stored as a list::
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
:func:`groupby` is roughly equivalent to::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
class groupby:
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# [k for k, g in groupby('AAAABBBCCDAABBB')] → A B C D A B
|
|
|
|
|
# [list(g) for k, g in groupby('AAAABBBCCD')] → AAAA BBB CC D
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def __init__(self, iterable, key=None):
|
|
|
|
|
if key is None:
|
|
|
|
|
key = lambda x: x
|
|
|
|
|
self.keyfunc = key
|
|
|
|
|
self.it = iter(iterable)
|
|
|
|
|
self.tgtkey = self.currkey = self.currvalue = object()
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def __iter__(self):
|
|
|
|
|
return self
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def __next__(self):
|
2017-09-24 07:36:11 -03:00
|
|
|
|
self.id = object()
|
2010-12-01 19:45:20 -04:00
|
|
|
|
while self.currkey == self.tgtkey:
|
|
|
|
|
self.currvalue = next(self.it) # Exit on StopIteration
|
|
|
|
|
self.currkey = self.keyfunc(self.currvalue)
|
|
|
|
|
self.tgtkey = self.currkey
|
2017-09-24 07:36:11 -03:00
|
|
|
|
return (self.currkey, self._grouper(self.tgtkey, self.id))
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2017-09-24 07:36:11 -03:00
|
|
|
|
def _grouper(self, tgtkey, id):
|
|
|
|
|
while self.id is id and self.currkey == tgtkey:
|
2010-12-01 19:45:20 -04:00
|
|
|
|
yield self.currvalue
|
2014-11-23 01:56:23 -04:00
|
|
|
|
try:
|
|
|
|
|
self.currvalue = next(self.it)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
return
|
2010-12-01 19:45:20 -04:00
|
|
|
|
self.currkey = self.keyfunc(self.currvalue)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
2012-09-14 00:32:35 -03:00
|
|
|
|
.. function:: islice(iterable, stop)
|
|
|
|
|
islice(iterable, start, stop[, step])
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns selected elements from the iterable. If *start* is
|
|
|
|
|
non-zero, then elements from the iterable are skipped until start is reached.
|
|
|
|
|
Afterward, elements are returned consecutively unless *step* is set higher than
|
|
|
|
|
one which results in items being skipped. If *stop* is ``None``, then iteration
|
|
|
|
|
continues until the iterator is exhausted, if at all; otherwise, it stops at the
|
2022-10-18 16:09:34 -03:00
|
|
|
|
specified position.
|
|
|
|
|
|
|
|
|
|
If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
|
|
|
|
|
then the step defaults to one.
|
|
|
|
|
|
|
|
|
|
Unlike regular slicing, :func:`islice` does not support negative values for
|
|
|
|
|
*start*, *stop*, or *step*. Can be used to extract related fields from
|
|
|
|
|
data where the internal structure has been flattened (for example, a
|
|
|
|
|
multi-line report may list a name field on every third line).
|
|
|
|
|
|
|
|
|
|
Roughly equivalent to::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
def islice(iterable, *args):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# islice('ABCDEFG', 2) → A B
|
|
|
|
|
# islice('ABCDEFG', 2, 4) → C D
|
|
|
|
|
# islice('ABCDEFG', 2, None) → C D E F G
|
|
|
|
|
# islice('ABCDEFG', 0, None, 2) → A C E G
|
2010-12-01 19:45:20 -04:00
|
|
|
|
s = slice(*args)
|
2018-03-26 22:29:33 -03:00
|
|
|
|
start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1
|
|
|
|
|
it = iter(range(start, stop, step))
|
2014-11-23 01:56:23 -04:00
|
|
|
|
try:
|
|
|
|
|
nexti = next(it)
|
|
|
|
|
except StopIteration:
|
2018-03-26 22:29:33 -03:00
|
|
|
|
# Consume *iterable* up to the *start* position.
|
|
|
|
|
for i, element in zip(range(start), iterable):
|
|
|
|
|
pass
|
2014-11-23 01:56:23 -04:00
|
|
|
|
return
|
2018-03-26 22:29:33 -03:00
|
|
|
|
try:
|
|
|
|
|
for i, element in enumerate(iterable):
|
|
|
|
|
if i == nexti:
|
|
|
|
|
yield element
|
|
|
|
|
nexti = next(it)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
# Consume to *stop*.
|
|
|
|
|
for i, element in zip(range(i + 1, stop), iterable):
|
|
|
|
|
pass
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2020-12-01 00:42:54 -04:00
|
|
|
|
.. function:: pairwise(iterable)
|
|
|
|
|
|
|
|
|
|
Return successive overlapping pairs taken from the input *iterable*.
|
|
|
|
|
|
|
|
|
|
The number of 2-tuples in the output iterator will be one fewer than the
|
|
|
|
|
number of inputs. It will be empty if the input iterable has fewer than
|
|
|
|
|
two values.
|
|
|
|
|
|
|
|
|
|
Roughly equivalent to::
|
|
|
|
|
|
|
|
|
|
def pairwise(iterable):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# pairwise('ABCDEFG') → AB BC CD DE EF FG
|
|
|
|
|
iterator = iter(iterable)
|
|
|
|
|
a = next(iterator, None)
|
|
|
|
|
for b in iterator:
|
|
|
|
|
yield a, b
|
|
|
|
|
a = b
|
2020-12-01 00:42:54 -04:00
|
|
|
|
|
2021-09-07 14:29:00 -03:00
|
|
|
|
.. versionadded:: 3.10
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: permutations(iterable, r=None)
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Return successive *r* length permutations of elements in the *iterable*.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
If *r* is not specified or is ``None``, then *r* defaults to the length
|
|
|
|
|
of the *iterable* and all possible full-length permutations
|
|
|
|
|
are generated.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2022-10-18 16:09:34 -03:00
|
|
|
|
The permutation tuples are emitted in lexicographic order according to
|
2020-05-28 16:56:43 -03:00
|
|
|
|
the order of the input *iterable*. So, if the input *iterable* is sorted,
|
2022-10-18 16:09:34 -03:00
|
|
|
|
the output tuples will be produced in sorted order.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Elements are treated as unique based on their position, not on their
|
2022-10-18 16:09:34 -03:00
|
|
|
|
value. So if the input elements are unique, there will be no repeated
|
|
|
|
|
values within a permutation.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to::
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
|
|
|
|
|
def permutations(iterable, r=None):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# permutations('ABCD', 2) → AB AC AD BA BC BD CA CB CD DA DB DC
|
|
|
|
|
# permutations(range(3)) → 012 021 102 120 201 210
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
r = n if r is None else r
|
2009-01-08 17:01:54 -04:00
|
|
|
|
if r > n:
|
|
|
|
|
return
|
|
|
|
|
indices = list(range(n))
|
2011-12-25 12:25:45 -04:00
|
|
|
|
cycles = list(range(n, n-r, -1))
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
yield tuple(pool[i] for i in indices[:r])
|
|
|
|
|
while n:
|
|
|
|
|
for i in reversed(range(r)):
|
|
|
|
|
cycles[i] -= 1
|
|
|
|
|
if cycles[i] == 0:
|
|
|
|
|
indices[i:] = indices[i+1:] + indices[i:i+1]
|
|
|
|
|
cycles[i] = n - i
|
|
|
|
|
else:
|
|
|
|
|
j = cycles[i]
|
|
|
|
|
indices[i], indices[-j] = indices[-j], indices[i]
|
|
|
|
|
yield tuple(pool[i] for i in indices[:r])
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
return
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The code for :func:`permutations` can be also expressed as a subsequence of
|
|
|
|
|
:func:`product`, filtered to exclude entries with repeated elements (those
|
|
|
|
|
from the same position in the input pool)::
|
Merged revisions 61209-61214,61217-61222,61224-61226,61233-61237 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61209 | georg.brandl | 2008-03-03 21:37:55 +0100 (Mon, 03 Mar 2008) | 2 lines
There are now sixteen isfoo functions.
........
r61210 | georg.brandl | 2008-03-03 21:39:00 +0100 (Mon, 03 Mar 2008) | 2 lines
15 -> 16, the 2nd
........
r61211 | georg.brandl | 2008-03-03 22:22:47 +0100 (Mon, 03 Mar 2008) | 2 lines
Actually import itertools.
........
r61212 | georg.brandl | 2008-03-03 22:31:50 +0100 (Mon, 03 Mar 2008) | 2 lines
Expand a bit on genexp scopes.
........
r61213 | raymond.hettinger | 2008-03-03 23:04:55 +0100 (Mon, 03 Mar 2008) | 1 line
Remove dependency on itertools -- a simple genexp suffices.
........
r61214 | raymond.hettinger | 2008-03-03 23:19:58 +0100 (Mon, 03 Mar 2008) | 1 line
Issue 2226: Callable checked for the wrong abstract method.
........
r61217 | andrew.kuchling | 2008-03-04 01:40:32 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61218 | andrew.kuchling | 2008-03-04 02:30:10 +0100 (Tue, 04 Mar 2008) | 1 line
Grammar fix; markup fix
........
r61219 | andrew.kuchling | 2008-03-04 02:47:38 +0100 (Tue, 04 Mar 2008) | 1 line
Fix sentence fragment
........
r61220 | andrew.kuchling | 2008-03-04 02:48:26 +0100 (Tue, 04 Mar 2008) | 1 line
Typo fix
........
r61221 | andrew.kuchling | 2008-03-04 02:49:37 +0100 (Tue, 04 Mar 2008) | 1 line
Add versionadded tags
........
r61222 | andrew.kuchling | 2008-03-04 02:50:32 +0100 (Tue, 04 Mar 2008) | 1 line
Thesis night results: add various items
........
r61224 | raymond.hettinger | 2008-03-04 05:17:08 +0100 (Tue, 04 Mar 2008) | 1 line
Beef-up docs and tests for itertools. Fix-up end-case for product().
........
r61225 | georg.brandl | 2008-03-04 08:25:54 +0100 (Tue, 04 Mar 2008) | 2 lines
Fix some patch attributions.
........
r61226 | georg.brandl | 2008-03-04 08:33:30 +0100 (Tue, 04 Mar 2008) | 2 lines
#2230: document that PyArg_* leaves addresses alone on error.
........
r61233 | neal.norwitz | 2008-03-04 17:22:46 +0100 (Tue, 04 Mar 2008) | 3 lines
Close the file before trying to remove the directory so it works on Windows.
As reported by Trent Nelson on python-dev.
........
r61234 | thomas.heller | 2008-03-04 21:09:11 +0100 (Tue, 04 Mar 2008) | 9 lines
Merged changes from libffi3-branch.
The bundled libffi copy is now in sync with the recently released
libffi3.0.4 version, apart from some small changes to
Modules/_ctypes/libffi/configure.ac.
I gave up on using libffi3 files on os x.
Instead, static configuration with files from pyobjc is used.
........
r61235 | thomas.heller | 2008-03-04 21:21:42 +0100 (Tue, 04 Mar 2008) | 1 line
Try to fix the build for PY_LINUX.
........
r61236 | fred.drake | 2008-03-04 22:14:04 +0100 (Tue, 04 Mar 2008) | 2 lines
fix typo
........
r61237 | raymond.hettinger | 2008-03-04 23:29:44 +0100 (Tue, 04 Mar 2008) | 1 line
Fix refleak in chain().
........
2008-03-04 19:39:23 -04:00
|
|
|
|
|
|
|
|
|
def permutations(iterable, r=None):
|
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
r = n if r is None else r
|
|
|
|
|
for indices in product(range(n), repeat=r):
|
|
|
|
|
if len(set(indices)) == r:
|
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The number of items returned is ``n! / (n-r)!`` when ``0 <= r <= n``
|
|
|
|
|
or zero when ``r > n``.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61097,61103-61108 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61105 | andrew.kuchling | 2008-02-28 15:03:03 +0100 (Thu, 28 Feb 2008) | 1 line
#2169: make generated HTML more valid
........
r61106 | jeffrey.yasskin | 2008-02-28 19:03:15 +0100 (Thu, 28 Feb 2008) | 4 lines
Prevent SocketServer.ForkingMixIn from waiting on child processes that it
didn't create, in most cases. When there are max_children handlers running, it
will still wait for any child process, not just handler processes.
........
r61107 | raymond.hettinger | 2008-02-28 20:41:24 +0100 (Thu, 28 Feb 2008) | 1 line
Document impending updates to itertools.
........
r61108 | martin.v.loewis | 2008-02-28 20:44:22 +0100 (Thu, 28 Feb 2008) | 1 line
Add 2.6aN uuids.
........
2008-02-28 16:02:27 -04:00
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: product(*iterables, repeat=1)
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Cartesian product of input iterables.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Roughly equivalent to nested for-loops in a generator expression. For example,
|
2010-12-01 19:45:20 -04:00
|
|
|
|
``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
The nested loops cycle like an odometer with the rightmost element advancing
|
|
|
|
|
on every iteration. This pattern creates a lexicographic ordering so that if
|
|
|
|
|
the input's iterables are sorted, the product tuples are emitted in sorted
|
|
|
|
|
order.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
To compute the product of an iterable with itself, specify the number of
|
|
|
|
|
repetitions with the optional *repeat* keyword argument. For example,
|
|
|
|
|
``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
|
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61098 | jeffrey.yasskin | 2008-02-28 05:45:36 +0100 (Thu, 28 Feb 2008) | 7 lines
Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
which forbids constructing types that have it set. The effect is to speed
./python.exe -m timeit -s 'import abc' -s 'class Foo(object): __metaclass__ = abc.ABCMeta' 'Foo()'
up from 2.5us to 0.201us. This fixes issue 1762.
........
r61099 | jeffrey.yasskin | 2008-02-28 06:53:18 +0100 (Thu, 28 Feb 2008) | 3 lines
Speed test_socketserver up from 28.739s to 0.226s, simplify the logic, and make
sure all tests run even if some fail.
........
r61100 | jeffrey.yasskin | 2008-02-28 07:09:19 +0100 (Thu, 28 Feb 2008) | 21 lines
Thread.start() used sleep(0.000001) to make sure it didn't return before the
new thread had started. At least on my MacBook Pro, that wound up sleeping for
a full 10ms (probably 1 jiffy). By using an Event instead, we can be absolutely
certain that the thread has started, and return more quickly (217us).
Before:
$ ./python.exe -m timeit -s 'from threading import Thread' 't = Thread(); t.start(); t.join()'
100 loops, best of 3: 10.3 msec per loop
$ ./python.exe -m timeit -s 'from threading import Thread; t = Thread()' 't.isAlive()'
1000000 loops, best of 3: 0.47 usec per loop
After:
$ ./python.exe -m timeit -s 'from threading import Thread' 't = Thread(); t.start(); t.join()'
1000 loops, best of 3: 217 usec per loop
$ ./python.exe -m timeit -s 'from threading import Thread; t = Thread()' 't.isAlive()'
1000000 loops, best of 3: 0.86 usec per loop
To be fair, the 10ms isn't CPU time, and other threads including the spawned
one get to run during it. There are also some slightly more complicated ways to
get back the .4us in isAlive() if we want.
........
r61101 | raymond.hettinger | 2008-02-28 10:23:48 +0100 (Thu, 28 Feb 2008) | 1 line
Add repeat keyword argument to itertools.product().
........
r61102 | christian.heimes | 2008-02-28 12:18:49 +0100 (Thu, 28 Feb 2008) | 1 line
The empty tuple is usually a singleton with a much higher refcnt than 1
........
2008-02-28 08:27:11 -04:00
|
|
|
|
|
2016-05-28 04:17:54 -03:00
|
|
|
|
This function is roughly equivalent to the following code, except that the
|
2010-12-01 19:45:20 -04:00
|
|
|
|
actual implementation does not build up intermediate results in memory::
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def product(*args, repeat=1):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# product('ABCD', 'xy') → Ax Ay Bx By Cx Cy Dx Dy
|
|
|
|
|
# product(range(2), repeat=3) → 000 001 010 011 100 101 110 111
|
2010-12-01 19:45:20 -04:00
|
|
|
|
pools = [tuple(pool) for pool in args] * repeat
|
|
|
|
|
result = [[]]
|
|
|
|
|
for pool in pools:
|
|
|
|
|
result = [x+[y] for x in result for y in pool]
|
|
|
|
|
for prod in result:
|
|
|
|
|
yield tuple(prod)
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2020-05-28 13:46:22 -03:00
|
|
|
|
Before :func:`product` runs, it completely consumes the input iterables,
|
|
|
|
|
keeping pools of values in memory to generate the products. Accordingly,
|
2020-12-17 16:24:43 -04:00
|
|
|
|
it is only useful with finite inputs.
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line
Tests for bin() builtin. These need to get merged into py3k, which has no tests for bin.
........
r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line
Document itertools.product().
........
r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines
Improve the implementation of itertools.product()
* Fix-up issues pointed-out by Neal Norwitz.
* Add extensive comments.
* The lz->result variable is now a tuple instead of a list.
* Use fast macro getitem/setitem calls so most code is in-line.
* Re-use the result tuple if available (modify in-place instead of copy).
........
r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line
Add more comments
........
r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line
Add recipe using itertools.product().
........
r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines
Issue 1881. Increased the stack limit from 500 to 1500. Also added
a test for this (and because of this test you'll see in stderr a
message that parser.c sends before raising MemoryError).
Thanks Ralf Schmitt.
........
r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines
Issue 1776581. Minor corrections to smtplib, and two small tests.
Thanks Alan McIntyre.
........
r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines
Issue 1781. Now ConfigParser.add_section does not let you add a
DEFAULT section any more, because it duplicated sections with
the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
2008-02-23 09:18:03 -04:00
|
|
|
|
|
2009-06-01 16:16:52 -03:00
|
|
|
|
.. function:: repeat(object[, times])
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns *object* over and over again. Runs indefinitely
|
2022-10-18 16:09:34 -03:00
|
|
|
|
unless the *times* argument is specified.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
|
|
|
|
|
Roughly equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def repeat(object, times=None):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# repeat(10, 3) → 10 10 10
|
2010-12-01 19:45:20 -04:00
|
|
|
|
if times is None:
|
|
|
|
|
while True:
|
|
|
|
|
yield object
|
|
|
|
|
else:
|
|
|
|
|
for i in range(times):
|
|
|
|
|
yield object
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2012-02-01 13:07:40 -04:00
|
|
|
|
A common use for *repeat* is to supply a stream of constant values to *map*
|
2022-10-18 16:09:34 -03:00
|
|
|
|
or *zip*:
|
|
|
|
|
|
|
|
|
|
.. doctest::
|
2012-02-01 13:07:40 -04:00
|
|
|
|
|
|
|
|
|
>>> list(map(pow, range(10), repeat(2)))
|
|
|
|
|
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. function:: starmap(function, iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that computes the function using arguments obtained from
|
|
|
|
|
the iterable. Used instead of :func:`map` when argument parameters are already
|
2022-10-18 16:09:34 -03:00
|
|
|
|
grouped in tuples from a single iterable (when the data has been
|
|
|
|
|
"pre-zipped").
|
|
|
|
|
|
|
|
|
|
The difference between :func:`map` and :func:`starmap` parallels the
|
|
|
|
|
distinction between ``function(a,b)`` and ``function(*c)``. Roughly
|
|
|
|
|
equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def starmap(function, iterable):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# starmap(pow, [(2,5), (3,2), (10,3)]) → 32 9 1000
|
2010-12-01 19:45:20 -04:00
|
|
|
|
for args in iterable:
|
|
|
|
|
yield function(*args)
|
Merged revisions 59985-60000,60002,60005-60007,60009-60042 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59987 | raymond.hettinger | 2008-01-15 21:52:42 +0100 (Tue, 15 Jan 2008) | 1 line
Refactor if/elif chain for clarity and speed. Remove dependency on subclasses having to implement _empty and _full.
........
r59988 | raymond.hettinger | 2008-01-15 22:22:47 +0100 (Tue, 15 Jan 2008) | 1 line
Fix-up half-written paragraph in the docs
........
r59989 | amaury.forgeotdarc | 2008-01-15 22:25:11 +0100 (Tue, 15 Jan 2008) | 3 lines
test_doctest fails since r59984.
Not sure if these are the correct values, but save_stdout has to be set before its usage...
........
r59992 | andrew.kuchling | 2008-01-16 01:32:03 +0100 (Wed, 16 Jan 2008) | 1 line
Docstring typos
........
r59993 | andrew.kuchling | 2008-01-16 04:17:25 +0100 (Wed, 16 Jan 2008) | 1 line
Add PEP 3141 section
........
r59998 | andrew.kuchling | 2008-01-16 14:01:51 +0100 (Wed, 16 Jan 2008) | 1 line
Markup fix
........
r59999 | georg.brandl | 2008-01-16 17:56:29 +0100 (Wed, 16 Jan 2008) | 2 lines
Fix MSDN library URL. (#1854)
........
r60006 | georg.brandl | 2008-01-16 21:27:56 +0100 (Wed, 16 Jan 2008) | 3 lines
Add Python-specific content to Doc dir. Update configuration file
to work with the newest Sphinx.
........
r60007 | georg.brandl | 2008-01-16 21:29:00 +0100 (Wed, 16 Jan 2008) | 2 lines
Doc build should work with 2.4 now.
........
r60009 | raymond.hettinger | 2008-01-17 00:38:16 +0100 (Thu, 17 Jan 2008) | 1 line
Minor wordsmithing.
........
r60010 | raymond.hettinger | 2008-01-17 00:40:45 +0100 (Thu, 17 Jan 2008) | 1 line
Add queues will alternative fetch orders (priority based and stack based).
........
r60011 | raymond.hettinger | 2008-01-17 00:49:35 +0100 (Thu, 17 Jan 2008) | 1 line
Add news entry.
........
r60013 | raymond.hettinger | 2008-01-17 04:02:14 +0100 (Thu, 17 Jan 2008) | 1 line
Make starmap() match its pure python definition and accept any itertable input (not just tuples).
........
r60015 | gregory.p.smith | 2008-01-17 08:43:20 +0100 (Thu, 17 Jan 2008) | 3 lines
Comply with RFC 3207.
Fixes issue 829951 - http://bugs.python.org/issue829951
........
r60018 | gregory.p.smith | 2008-01-17 09:03:17 +0100 (Thu, 17 Jan 2008) | 2 lines
entry for r60015
........
r60019 | raymond.hettinger | 2008-01-17 09:07:05 +0100 (Thu, 17 Jan 2008) | 1 line
Note versionadded.
........
r60020 | gregory.p.smith | 2008-01-17 09:35:49 +0100 (Thu, 17 Jan 2008) | 8 lines
Fixes (accepts patch) issue1339 - http://bugs.python.org/issue1339
- Factor out the duplication of EHLO/HELO in login() and sendmail() to
a new function, ehlo_or_helo_if_needed().
- Use ehlo_or_helo_if_needed() in starttls()
- Check for the starttls exception in starttls() in the same way as
login() checks for the auth extension.
Contributed by Bill Fenner.
........
r60021 | andrew.kuchling | 2008-01-17 13:00:15 +0100 (Thu, 17 Jan 2008) | 1 line
Revise 3141 section a bit; add some Windows items
........
r60022 | brett.cannon | 2008-01-17 19:45:10 +0100 (Thu, 17 Jan 2008) | 2 lines
Fix a function pointer declaration to silence the compiler.
........
r60024 | raymond.hettinger | 2008-01-17 20:31:38 +0100 (Thu, 17 Jan 2008) | 1 line
Issue #1861: Add read-only attribute listing upcoming events in the order they will be run.
........
r60025 | andrew.kuchling | 2008-01-17 20:49:24 +0100 (Thu, 17 Jan 2008) | 1 line
Correction from Jordan Lewis: halfdelay() uses tenths of a second, not milliseconds
........
r60026 | raymond.hettinger | 2008-01-17 23:27:49 +0100 (Thu, 17 Jan 2008) | 1 line
Add advice on choosing between scheduler and threading.Timer().
........
r60028 | christian.heimes | 2008-01-18 00:01:44 +0100 (Fri, 18 Jan 2008) | 2 lines
Updated new property syntax. An elaborate example for subclassing and the getter was missing.
Added comment about VS 2008 and PGO builds.
........
r60029 | raymond.hettinger | 2008-01-18 00:32:01 +0100 (Fri, 18 Jan 2008) | 1 line
Fix-up Timer() example.
........
r60030 | raymond.hettinger | 2008-01-18 00:56:56 +0100 (Fri, 18 Jan 2008) | 1 line
Fix markup
........
r60031 | raymond.hettinger | 2008-01-18 01:10:42 +0100 (Fri, 18 Jan 2008) | 1 line
clearcache() needs to remove the dict as well as clear it.
........
r60033 | andrew.kuchling | 2008-01-18 03:26:16 +0100 (Fri, 18 Jan 2008) | 1 line
Bump verson
........
r60034 | andrew.kuchling | 2008-01-18 03:42:52 +0100 (Fri, 18 Jan 2008) | 1 line
Typo fix
........
r60035 | christian.heimes | 2008-01-18 08:30:20 +0100 (Fri, 18 Jan 2008) | 3 lines
Coverity issue CID #197
var_decl: Declared variable "stm" without initializer
ninit_use_in_call: Using uninitialized value "stm" (field "stm".tm_zone uninitialized) in call to function "mktime"
........
r60036 | christian.heimes | 2008-01-18 08:45:30 +0100 (Fri, 18 Jan 2008) | 11 lines
Coverity issue CID #167
Event alloc_fn: Called allocation function "metacompile" [model]
Event var_assign: Assigned variable "gr" to storage returned from "metacompile"
gr = metacompile(n);
Event pass_arg: Variable "gr" not freed or pointed-to in function "maketables" [model]
g = maketables(gr);
translatelabels(g);
addfirstsets(g);
Event leaked_storage: Returned without freeing storage "gr"
return g;
........
r60038 | christian.heimes | 2008-01-18 09:04:57 +0100 (Fri, 18 Jan 2008) | 3 lines
Coverity issue CID #182
size_error: Allocating 1 bytes to pointer "children", which needs at least 4 bytes
........
r60041 | christian.heimes | 2008-01-18 09:47:59 +0100 (Fri, 18 Jan 2008) | 4 lines
Coverity issue CID #169
local_ptr_assign_local: Assigning address of stack variable "namebuf" to pointer "filename"
out_of_scope: Variable "namebuf" goes out of scope
use_invalid: Used "filename" pointing to out-of-scope variable "namebuf"
........
r60042 | christian.heimes | 2008-01-18 09:53:45 +0100 (Fri, 18 Jan 2008) | 2 lines
Coverity CID #168
leaked_storage: Returned without freeing storage "fp"
........
2008-01-18 05:56:22 -04:00
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
.. function:: takewhile(predicate, iterable)
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that returns elements from the iterable as long as the
|
2016-05-28 04:17:54 -03:00
|
|
|
|
predicate is true. Roughly equivalent to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def takewhile(predicate, iterable):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# takewhile(lambda x: x<5, [1,4,6,4,1]) → 1 4
|
2010-12-01 19:45:20 -04:00
|
|
|
|
for x in iterable:
|
|
|
|
|
if predicate(x):
|
|
|
|
|
yield x
|
|
|
|
|
else:
|
|
|
|
|
break
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2024-02-25 08:32:14 -04:00
|
|
|
|
Note, the element that first fails the predicate condition is
|
|
|
|
|
consumed from the input iterator and there is no way to access it.
|
|
|
|
|
This could be an issue if an application wants to further consume the
|
|
|
|
|
input iterator after takewhile has been run to exhaustion. To work
|
|
|
|
|
around this problem, consider using `more-iterools before_and_after()
|
|
|
|
|
<https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.before_and_after>`_
|
|
|
|
|
instead.
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: tee(iterable, n=2)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2016-04-26 04:10:00 -03:00
|
|
|
|
Return *n* independent iterators from a single iterable.
|
|
|
|
|
|
|
|
|
|
The following Python code helps explain what *tee* does (although the actual
|
2016-05-16 03:31:54 -03:00
|
|
|
|
implementation is more complex and uses only a single underlying
|
2022-10-18 16:09:34 -03:00
|
|
|
|
:abbr:`FIFO (first-in, first-out)` queue)::
|
2009-02-18 16:56:51 -04:00
|
|
|
|
|
|
|
|
|
def tee(iterable, n=2):
|
|
|
|
|
it = iter(iterable)
|
|
|
|
|
deques = [collections.deque() for i in range(n)]
|
|
|
|
|
def gen(mydeque):
|
|
|
|
|
while True:
|
|
|
|
|
if not mydeque: # when the local deque is empty
|
2014-11-23 01:56:23 -04:00
|
|
|
|
try:
|
|
|
|
|
newval = next(it) # fetch a new value and
|
|
|
|
|
except StopIteration:
|
|
|
|
|
return
|
2009-02-18 16:56:51 -04:00
|
|
|
|
for d in deques: # load it to all the deques
|
|
|
|
|
d.append(newval)
|
|
|
|
|
yield mydeque.popleft()
|
|
|
|
|
return tuple(gen(d) for d in deques)
|
|
|
|
|
|
2022-10-18 16:09:34 -03:00
|
|
|
|
Once a :func:`tee` has been created, the original *iterable* should not be
|
2010-12-01 19:45:20 -04:00
|
|
|
|
used anywhere else; otherwise, the *iterable* could get advanced without
|
2019-09-09 05:18:16 -03:00
|
|
|
|
the tee objects being informed.
|
2009-02-18 16:56:51 -04:00
|
|
|
|
|
2019-09-09 05:47:14 -03:00
|
|
|
|
``tee`` iterators are not threadsafe. A :exc:`RuntimeError` may be
|
2022-08-19 03:20:45 -03:00
|
|
|
|
raised when simultaneously using iterators returned by the same :func:`tee`
|
2019-09-09 05:47:14 -03:00
|
|
|
|
call, even if the original *iterable* is threadsafe.
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
This itertool may require significant auxiliary storage (depending on how
|
|
|
|
|
much temporary data needs to be stored). In general, if one iterator uses
|
|
|
|
|
most or all of the data before another iterator starts, it is faster to use
|
|
|
|
|
:func:`list` instead of :func:`tee`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
2009-06-01 14:35:27 -03:00
|
|
|
|
.. function:: zip_longest(*iterables, fillvalue=None)
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
Make an iterator that aggregates elements from each of the iterables. If the
|
|
|
|
|
iterables are of uneven length, missing values are filled-in with *fillvalue*.
|
2016-05-28 04:17:54 -03:00
|
|
|
|
Iteration continues until the longest iterable is exhausted. Roughly equivalent to::
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2017-09-07 18:01:44 -03:00
|
|
|
|
def zip_longest(*args, fillvalue=None):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# zip_longest('ABCD', 'xy', fillvalue='-') → Ax By C- D-
|
2017-09-07 18:01:44 -03:00
|
|
|
|
iterators = [iter(it) for it in args]
|
|
|
|
|
num_active = len(iterators)
|
|
|
|
|
if not num_active:
|
|
|
|
|
return
|
|
|
|
|
while True:
|
|
|
|
|
values = []
|
|
|
|
|
for i, it in enumerate(iterators):
|
|
|
|
|
try:
|
|
|
|
|
value = next(it)
|
|
|
|
|
except StopIteration:
|
|
|
|
|
num_active -= 1
|
|
|
|
|
if not num_active:
|
|
|
|
|
return
|
|
|
|
|
iterators[i] = repeat(fillvalue)
|
|
|
|
|
value = fillvalue
|
|
|
|
|
values.append(value)
|
|
|
|
|
yield tuple(values)
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
If one of the iterables is potentially infinite, then the :func:`zip_longest`
|
|
|
|
|
function should be wrapped with something that limits the number of calls
|
|
|
|
|
(for example :func:`islice` or :func:`takewhile`). If not specified,
|
|
|
|
|
*fillvalue* defaults to ``None``.
|
2009-01-27 00:42:48 -04:00
|
|
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
.. _itertools-recipes:
|
|
|
|
|
|
2010-12-06 19:31:36 -04:00
|
|
|
|
Itertools Recipes
|
|
|
|
|
-----------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
This section shows recipes for creating an extended toolset using the existing
|
|
|
|
|
itertools as building blocks.
|
|
|
|
|
|
2022-10-18 16:09:34 -03:00
|
|
|
|
The primary purpose of the itertools recipes is educational. The recipes show
|
|
|
|
|
various ways of thinking about individual tools — for example, that
|
|
|
|
|
``chain.from_iterable`` is related to the concept of flattening. The recipes
|
|
|
|
|
also give ideas about ways that the tools can be combined — for example, how
|
2024-02-28 19:11:05 -04:00
|
|
|
|
``starmap()`` and ``repeat()`` can work together. The recipes also show patterns
|
2022-10-18 16:09:34 -03:00
|
|
|
|
for using itertools with the :mod:`operator` and :mod:`collections` modules as
|
|
|
|
|
well as with the built-in itertools such as ``map()``, ``filter()``,
|
|
|
|
|
``reversed()``, and ``enumerate()``.
|
|
|
|
|
|
|
|
|
|
A secondary purpose of the recipes is to serve as an incubator. The
|
|
|
|
|
``accumulate()``, ``compress()``, and ``pairwise()`` itertools started out as
|
2024-03-13 17:02:56 -03:00
|
|
|
|
recipes. Currently, the ``sliding_window()``, ``iter_index()``, and ``sieve()``
|
|
|
|
|
recipes are being tested to see whether they prove their worth.
|
2022-10-18 16:09:34 -03:00
|
|
|
|
|
2019-08-04 17:35:58 -03:00
|
|
|
|
Substantially all of these recipes and many, many others can be installed from
|
|
|
|
|
the `more-itertools project <https://pypi.org/project/more-itertools/>`_ found
|
|
|
|
|
on the Python Package Index::
|
|
|
|
|
|
2022-04-09 15:37:01 -03:00
|
|
|
|
python -m pip install more-itertools
|
2019-08-04 17:35:58 -03:00
|
|
|
|
|
2022-10-18 16:09:34 -03:00
|
|
|
|
Many of the recipes offer the same high performance as the underlying toolset.
|
2024-03-13 17:02:56 -03:00
|
|
|
|
Superior memory performance is kept by processing elements one at a time rather
|
|
|
|
|
than bringing the whole iterable into memory all at once. Code volume is kept
|
|
|
|
|
small by linking the tools together in a `functional style
|
|
|
|
|
<https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`_. High speed
|
|
|
|
|
is retained by preferring "vectorized" building blocks over the use of for-loops
|
|
|
|
|
and :term:`generators <generator>` which incur interpreter overhead.
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
|
|
|
|
|
|
.. testcode::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
2022-12-24 04:21:30 -04:00
|
|
|
|
import collections
|
2023-04-28 14:25:50 -03:00
|
|
|
|
import functools
|
2022-12-24 04:21:30 -04:00
|
|
|
|
import math
|
|
|
|
|
import operator
|
|
|
|
|
import random
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def take(n, iterable):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Return first n items of the iterable as a list."
|
2010-12-01 19:45:20 -04:00
|
|
|
|
return list(islice(iterable, n))
|
|
|
|
|
|
2023-11-22 01:35:36 -04:00
|
|
|
|
def prepend(value, iterable):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Prepend a single value in front of an iterable."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# prepend(1, [2, 3, 4]) → 1 2 3 4
|
2023-11-22 01:35:36 -04:00
|
|
|
|
return chain([value], iterable)
|
2018-04-08 12:44:20 -03:00
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
def tabulate(function, start=0):
|
|
|
|
|
"Return function(0), function(1), ..."
|
|
|
|
|
return map(function, count(start))
|
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
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))
|
|
|
|
|
return starmap(func, repeat(args, times))
|
|
|
|
|
|
|
|
|
|
def flatten(list_of_lists):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Flatten one level of nesting."
|
2023-04-09 16:17:37 -03:00
|
|
|
|
return chain.from_iterable(list_of_lists)
|
|
|
|
|
|
|
|
|
|
def ncycles(iterable, n):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Returns the sequence elements n times."
|
2023-04-09 16:17:37 -03:00
|
|
|
|
return chain.from_iterable(repeat(tuple(iterable), n))
|
|
|
|
|
|
2014-05-26 02:03:56 -03:00
|
|
|
|
def tail(n, iterable):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Return an iterator over the last n items."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# tail(3, 'ABCDEFG') → E F G
|
2014-05-26 02:03:56 -03:00
|
|
|
|
return iter(collections.deque(iterable, maxlen=n))
|
|
|
|
|
|
2018-03-26 22:29:33 -03:00
|
|
|
|
def consume(iterator, n=None):
|
|
|
|
|
"Advance the iterator n-steps ahead. If n is None, consume entirely."
|
2010-12-01 19:45:20 -04:00
|
|
|
|
# Use functions that consume iterators at C speed.
|
|
|
|
|
if n is None:
|
|
|
|
|
# feed the entire iterator into a zero-length deque
|
|
|
|
|
collections.deque(iterator, maxlen=0)
|
|
|
|
|
else:
|
|
|
|
|
# advance to the empty slice starting at position n
|
|
|
|
|
next(islice(iterator, n, n), None)
|
|
|
|
|
|
|
|
|
|
def nth(iterable, n, default=None):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Returns the nth item or a default value."
|
2010-12-01 19:45:20 -04:00
|
|
|
|
return next(islice(iterable, n, None), default)
|
|
|
|
|
|
2024-03-04 11:51:29 -04:00
|
|
|
|
def quantify(iterable, predicate=bool):
|
2023-09-19 09:39:44 -03:00
|
|
|
|
"Given a predicate that returns True or False, count the True results."
|
2024-03-04 11:51:29 -04:00
|
|
|
|
return sum(map(predicate, iterable))
|
|
|
|
|
|
|
|
|
|
def first_true(iterable, default=False, predicate=None):
|
|
|
|
|
"Returns the first true value or the *default* if there is no true value."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# first_true([a,b,c], x) → a or b or c or x
|
|
|
|
|
# first_true([a,b], x, f) → a if f(a) else b if f(b) else x
|
2024-03-04 11:51:29 -04:00
|
|
|
|
return next(filter(predicate, iterable), default)
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2024-02-28 19:04:56 -04:00
|
|
|
|
def all_equal(iterable, key=None):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Returns True if all the elements are equal to each other."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# all_equal('4٤໔4৪', key=int) → True
|
2024-02-28 19:04:56 -04:00
|
|
|
|
return len(take(2, groupby(iterable, key))) <= 1
|
2020-12-25 20:43:20 -04:00
|
|
|
|
|
2024-03-13 17:02:56 -03:00
|
|
|
|
def unique_justseen(iterable, key=None):
|
|
|
|
|
"List unique elements, preserving order. Remember only the element just seen."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# unique_justseen('AAAABBBCCDAABBB') → A B C D A B
|
|
|
|
|
# unique_justseen('ABBcCAD', str.casefold) → A B c A D
|
2024-03-13 17:02:56 -03:00
|
|
|
|
if key is None:
|
|
|
|
|
return map(operator.itemgetter(0), groupby(iterable))
|
|
|
|
|
return map(next, map(operator.itemgetter(1), groupby(iterable, key)))
|
|
|
|
|
|
2023-12-25 18:26:04 -04:00
|
|
|
|
def unique_everseen(iterable, key=None):
|
|
|
|
|
"List unique elements, preserving order. Remember all elements ever seen."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# unique_everseen('AAAABBBCCDAABBB') → A B C D
|
|
|
|
|
# unique_everseen('ABBcCAD', str.casefold) → A B c D
|
2023-12-25 18:26:04 -04:00
|
|
|
|
seen = set()
|
|
|
|
|
if key is None:
|
|
|
|
|
for element in filterfalse(seen.__contains__, iterable):
|
|
|
|
|
seen.add(element)
|
|
|
|
|
yield element
|
|
|
|
|
else:
|
|
|
|
|
for element in iterable:
|
|
|
|
|
k = key(element)
|
|
|
|
|
if k not in seen:
|
|
|
|
|
seen.add(k)
|
|
|
|
|
yield element
|
|
|
|
|
|
|
|
|
|
def sliding_window(iterable, n):
|
|
|
|
|
"Collect data into overlapping fixed-length chunks or blocks."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG
|
2023-12-25 18:26:04 -04:00
|
|
|
|
it = iter(iterable)
|
|
|
|
|
window = collections.deque(islice(it, n-1), maxlen=n)
|
|
|
|
|
for x in it:
|
|
|
|
|
window.append(x)
|
|
|
|
|
yield tuple(window)
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2022-01-23 16:31:10 -04:00
|
|
|
|
def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Collect data into non-overlapping fixed-length chunks or blocks."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# grouper('ABCDEFG', 3, fillvalue='x') → ABC DEF Gxx
|
|
|
|
|
# grouper('ABCDEFG', 3, incomplete='strict') → ABC DEF ValueError
|
|
|
|
|
# grouper('ABCDEFG', 3, incomplete='ignore') → ABC DEF
|
2024-03-13 04:12:30 -03:00
|
|
|
|
iterators = [iter(iterable)] * n
|
2023-12-13 14:11:52 -04:00
|
|
|
|
match incomplete:
|
|
|
|
|
case 'fill':
|
2024-03-13 04:12:30 -03:00
|
|
|
|
return zip_longest(*iterators, fillvalue=fillvalue)
|
2023-12-13 14:11:52 -04:00
|
|
|
|
case 'strict':
|
2024-03-13 04:12:30 -03:00
|
|
|
|
return zip(*iterators, strict=True)
|
2023-12-13 14:11:52 -04:00
|
|
|
|
case 'ignore':
|
2024-03-13 04:12:30 -03:00
|
|
|
|
return zip(*iterators)
|
2023-12-13 14:11:52 -04:00
|
|
|
|
case _:
|
|
|
|
|
raise ValueError('Expected fill, strict, or ignore')
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
|
|
|
|
def roundrobin(*iterables):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Visit input iterables in a cycle until each is exhausted."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# roundrobin('ABC', 'D', 'EF') → A D E B F C
|
2024-03-13 04:12:30 -03:00
|
|
|
|
# Algorithm credited to George Sakkis
|
|
|
|
|
iterators = map(iter, iterables)
|
|
|
|
|
for num_active in range(len(iterables), 0, -1):
|
|
|
|
|
iterators = cycle(islice(iterators, num_active))
|
|
|
|
|
yield from map(next, iterators)
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2024-03-04 11:51:29 -04:00
|
|
|
|
def partition(predicate, iterable):
|
2023-05-25 00:31:02 -03:00
|
|
|
|
"""Partition entries into false entries and true entries.
|
|
|
|
|
|
2024-03-04 11:51:29 -04:00
|
|
|
|
If *predicate* is slow, consider wrapping it with functools.lru_cache().
|
2023-05-25 00:31:02 -03:00
|
|
|
|
"""
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# partition(is_odd, range(10)) → 0 2 4 6 8 and 1 3 5 7 9
|
2010-12-01 19:45:20 -04:00
|
|
|
|
t1, t2 = tee(iterable)
|
2024-03-04 11:51:29 -04:00
|
|
|
|
return filterfalse(predicate, t1), filter(predicate, t2)
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
def subslices(seq):
|
2023-12-25 18:26:04 -04:00
|
|
|
|
"Return all contiguous non-empty subslices of a sequence."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# subslices('ABCD') → A AB ABC ABCD B BC BCD C CD D
|
2023-04-09 16:17:37 -03:00
|
|
|
|
slices = starmap(slice, combinations(range(len(seq) + 1), 2))
|
|
|
|
|
return map(operator.getitem, repeat(seq), slices)
|
|
|
|
|
|
2024-03-13 17:02:56 -03:00
|
|
|
|
def iter_index(iterable, value, start=0, stop=None):
|
|
|
|
|
"Return indices where a value occurs in a sequence or iterable."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# iter_index('AABCADEAF', 'A') → 0 1 4 7
|
2024-03-13 17:02:56 -03:00
|
|
|
|
seq_index = getattr(iterable, 'index', None)
|
|
|
|
|
if seq_index is None:
|
|
|
|
|
# Path for general iterables
|
|
|
|
|
it = islice(iterable, start, stop)
|
|
|
|
|
for i, element in enumerate(it, start):
|
|
|
|
|
if element is value or element == value:
|
|
|
|
|
yield i
|
|
|
|
|
else:
|
|
|
|
|
# Path for sequences with an index() method
|
|
|
|
|
stop = len(iterable) if stop is None else stop
|
|
|
|
|
i = start
|
|
|
|
|
try:
|
|
|
|
|
while True:
|
|
|
|
|
yield (i := seq_index(value, i, stop))
|
|
|
|
|
i += 1
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass
|
|
|
|
|
|
2023-12-25 18:26:04 -04:00
|
|
|
|
def iter_except(func, exception, first=None):
|
|
|
|
|
""" Call a function repeatedly until an exception is raised.
|
|
|
|
|
|
|
|
|
|
Converts a call-until-exception interface to an iterator interface.
|
|
|
|
|
"""
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# iter_except(d.popitem, KeyError) → non-blocking dictionary iterator
|
2023-12-25 18:26:04 -04:00
|
|
|
|
try:
|
|
|
|
|
if first is not None:
|
|
|
|
|
yield first()
|
|
|
|
|
while True:
|
|
|
|
|
yield func()
|
|
|
|
|
except exception:
|
|
|
|
|
pass
|
|
|
|
|
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
The following recipes have a more mathematical flavor:
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
.. testcode::
|
2010-12-01 19:45:20 -04:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
def powerset(iterable):
|
2024-03-14 18:39:50 -03:00
|
|
|
|
"powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
|
2023-04-09 16:17:37 -03:00
|
|
|
|
s = list(iterable)
|
|
|
|
|
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
|
|
|
|
|
|
2024-03-13 04:12:30 -03:00
|
|
|
|
def sum_of_squares(iterable):
|
2023-04-09 16:17:37 -03:00
|
|
|
|
"Add up the squares of the input values."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# sum_of_squares([10, 20, 30]) → 1400
|
2024-03-13 04:12:30 -03:00
|
|
|
|
return math.sumprod(*tee(iterable))
|
2014-04-02 07:16:42 -03:00
|
|
|
|
|
2023-12-15 20:03:44 -04:00
|
|
|
|
def reshape(matrix, cols):
|
|
|
|
|
"Reshape a 2-D matrix to have a given number of columns."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# reshape([(0, 1), (2, 3), (4, 5)], 3) → (0, 1, 2), (3, 4, 5)
|
2023-12-16 11:13:50 -04:00
|
|
|
|
return batched(chain.from_iterable(matrix), cols, strict=True)
|
2023-12-15 20:03:44 -04:00
|
|
|
|
|
|
|
|
|
def transpose(matrix):
|
|
|
|
|
"Swap the rows and columns of a 2-D matrix."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# transpose([(1, 2, 3), (11, 22, 33)]) → (1, 11) (2, 22) (3, 33)
|
2023-12-15 20:03:44 -04:00
|
|
|
|
return zip(*matrix, strict=True)
|
2014-04-02 07:16:42 -03:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
def matmul(m1, m2):
|
|
|
|
|
"Multiply two matrices."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]) → (49, 80), (41, 60)
|
2023-04-09 16:17:37 -03:00
|
|
|
|
n = len(m2[0])
|
|
|
|
|
return batched(starmap(math.sumprod, product(m1, transpose(m2))), n)
|
|
|
|
|
|
|
|
|
|
def convolve(signal, kernel):
|
2023-09-19 09:39:44 -03:00
|
|
|
|
"""Discrete linear convolution of two iterables.
|
2024-03-12 19:19:58 -03:00
|
|
|
|
Equivalent to polynomial multiplication.
|
2023-09-19 09:39:44 -03:00
|
|
|
|
|
2024-03-12 19:19:58 -03:00
|
|
|
|
Convolutions are mathematically commutative; however, the inputs are
|
|
|
|
|
evaluated differently. The signal is consumed lazily and can be
|
|
|
|
|
infinite. The kernel is fully consumed before the calculations begin.
|
2014-04-02 07:16:42 -03:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
Article: https://betterexplained.com/articles/intuitive-convolution/
|
|
|
|
|
Video: https://www.youtube.com/watch?v=KuXjwB4LzSA
|
2014-04-02 07:16:42 -03:00
|
|
|
|
"""
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# convolve([1, -1, -20], [1, -3]) → 1 -4 -17 60
|
|
|
|
|
# convolve(data, [0.25, 0.25, 0.25, 0.25]) → Moving average (blur)
|
|
|
|
|
# convolve(data, [1/2, 0, -1/2]) → 1st derivative estimate
|
|
|
|
|
# convolve(data, [1, -2, 1]) → 2nd derivative estimate
|
2023-04-09 16:17:37 -03:00
|
|
|
|
kernel = tuple(kernel)[::-1]
|
|
|
|
|
n = len(kernel)
|
2023-04-28 14:25:50 -03:00
|
|
|
|
padded_signal = chain(repeat(0, n-1), signal, repeat(0, n-1))
|
2023-07-03 17:38:38 -03:00
|
|
|
|
windowed_signal = sliding_window(padded_signal, n)
|
|
|
|
|
return map(math.sumprod, repeat(kernel), windowed_signal)
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
|
|
|
|
def polynomial_from_roots(roots):
|
|
|
|
|
"""Compute a polynomial's coefficients from its roots.
|
|
|
|
|
|
|
|
|
|
(x - 5) (x + 4) (x - 3) expands to: x³ -4x² -17x + 60
|
|
|
|
|
"""
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# polynomial_from_roots([5, -4, 3]) → [1, -4, -17, 60]
|
2023-04-28 14:25:50 -03:00
|
|
|
|
factors = zip(repeat(1), map(operator.neg, roots))
|
|
|
|
|
return list(functools.reduce(convolve, factors, [1]))
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
|
|
|
|
def polynomial_eval(coefficients, x):
|
|
|
|
|
"""Evaluate a polynomial at a specific value.
|
|
|
|
|
|
|
|
|
|
Computes with better numeric stability than Horner's method.
|
|
|
|
|
"""
|
2024-03-13 17:02:56 -03:00
|
|
|
|
# Evaluate x³ -4x² -17x + 60 at x = 5
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# polynomial_eval([1, -4, -17, 60], x=5) → 0
|
2023-04-09 16:17:37 -03:00
|
|
|
|
n = len(coefficients)
|
2023-09-19 09:39:44 -03:00
|
|
|
|
if not n:
|
|
|
|
|
return type(x)(0)
|
2023-04-09 16:17:37 -03:00
|
|
|
|
powers = map(pow, repeat(x), reversed(range(n)))
|
|
|
|
|
return math.sumprod(coefficients, powers)
|
2014-04-02 07:16:42 -03:00
|
|
|
|
|
2023-05-25 00:31:02 -03:00
|
|
|
|
def polynomial_derivative(coefficients):
|
|
|
|
|
"""Compute the first derivative of a polynomial.
|
|
|
|
|
|
|
|
|
|
f(x) = x³ -4x² -17x + 60
|
|
|
|
|
f'(x) = 3x² -8x -17
|
|
|
|
|
"""
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# polynomial_derivative([1, -4, -17, 60]) → [3, -8, -17]
|
2023-05-25 00:31:02 -03:00
|
|
|
|
n = len(coefficients)
|
|
|
|
|
powers = reversed(range(1, n))
|
|
|
|
|
return list(map(operator.mul, coefficients, powers))
|
|
|
|
|
|
2023-09-19 09:39:44 -03:00
|
|
|
|
def sieve(n):
|
|
|
|
|
"Primes less than n."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# sieve(30) → 2 3 5 7 11 13 17 19 23 29
|
2023-09-19 09:39:44 -03:00
|
|
|
|
if n > 2:
|
|
|
|
|
yield 2
|
|
|
|
|
start = 3
|
|
|
|
|
data = bytearray((0, 1)) * (n // 2)
|
|
|
|
|
limit = math.isqrt(n) + 1
|
|
|
|
|
for p in iter_index(data, 1, start, limit):
|
|
|
|
|
yield from iter_index(data, 1, start, p*p)
|
|
|
|
|
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
|
|
|
|
|
start = p*p
|
|
|
|
|
yield from iter_index(data, 1, start)
|
|
|
|
|
|
|
|
|
|
def factor(n):
|
|
|
|
|
"Prime factors of n."
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# factor(99) → 3 3 11
|
|
|
|
|
# factor(1_000_000_000_000_007) → 47 59 360620266859
|
|
|
|
|
# factor(1_000_000_000_000_403) → 1000000000000403
|
2023-09-19 09:39:44 -03:00
|
|
|
|
for prime in sieve(math.isqrt(n) + 1):
|
|
|
|
|
while not n % prime:
|
|
|
|
|
yield prime
|
|
|
|
|
n //= prime
|
|
|
|
|
if n == 1:
|
|
|
|
|
return
|
|
|
|
|
if n > 1:
|
|
|
|
|
yield n
|
|
|
|
|
|
2023-12-14 15:15:29 -04:00
|
|
|
|
def totient(n):
|
|
|
|
|
"Count of natural numbers up to n that are coprime to n."
|
|
|
|
|
# https://mathworld.wolfram.com/TotientFunction.html
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# totient(12) → 4 because len([1, 5, 7, 11]) == 4
|
2023-12-14 15:15:29 -04:00
|
|
|
|
for p in unique_justseen(factor(n)):
|
2024-01-08 15:16:22 -04:00
|
|
|
|
n -= n // p
|
2023-12-14 15:15:29 -04:00
|
|
|
|
return n
|
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
2022-01-25 08:56:53 -04:00
|
|
|
|
.. doctest::
|
|
|
|
|
:hide:
|
|
|
|
|
|
|
|
|
|
These examples no longer appear in the docs but are guaranteed
|
|
|
|
|
to keep working.
|
|
|
|
|
|
|
|
|
|
>>> amounts = [120.15, 764.05, 823.14]
|
|
|
|
|
>>> for checknum, amount in zip(count(1200), amounts):
|
|
|
|
|
... print('Check %d is for $%.2f' % (checknum, amount))
|
|
|
|
|
...
|
|
|
|
|
Check 1200 is for $120.15
|
|
|
|
|
Check 1201 is for $764.05
|
|
|
|
|
Check 1202 is for $823.14
|
|
|
|
|
|
|
|
|
|
>>> import operator
|
|
|
|
|
>>> for cube in map(operator.pow, range(1,4), repeat(3)):
|
|
|
|
|
... print(cube)
|
|
|
|
|
...
|
|
|
|
|
1
|
|
|
|
|
8
|
|
|
|
|
27
|
|
|
|
|
|
|
|
|
|
>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', '', 'martin', '', 'walter', '', 'samuele']
|
|
|
|
|
>>> for name in islice(reportlines, 3, None, 2):
|
|
|
|
|
... print(name.title())
|
|
|
|
|
...
|
|
|
|
|
Alex
|
|
|
|
|
Laura
|
|
|
|
|
Martin
|
|
|
|
|
Walter
|
|
|
|
|
Samuele
|
|
|
|
|
|
|
|
|
|
>>> from operator import itemgetter
|
|
|
|
|
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
|
|
|
|
|
>>> di = sorted(sorted(d.items()), key=itemgetter(1))
|
|
|
|
|
>>> for k, g in groupby(di, itemgetter(1)):
|
|
|
|
|
... print(k, list(map(itemgetter(0), g)))
|
|
|
|
|
...
|
|
|
|
|
1 ['a', 'c', 'e']
|
|
|
|
|
2 ['b', 'd', 'f']
|
|
|
|
|
3 ['g']
|
|
|
|
|
|
|
|
|
|
# Find runs of consecutive numbers using groupby. The key to the solution
|
|
|
|
|
# is differencing with a range so that consecutive numbers all appear in
|
|
|
|
|
# same group.
|
|
|
|
|
>>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
|
|
|
|
|
>>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
|
|
|
|
|
... print(list(map(operator.itemgetter(1), g)))
|
|
|
|
|
...
|
|
|
|
|
[1]
|
|
|
|
|
[4, 5, 6]
|
|
|
|
|
[10]
|
|
|
|
|
[15, 16, 17, 18]
|
|
|
|
|
[22]
|
|
|
|
|
[25, 26, 27, 28]
|
|
|
|
|
|
|
|
|
|
Now, we test all of the itertool recipes
|
|
|
|
|
|
|
|
|
|
>>> take(10, count())
|
|
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify that the input is consumed lazily
|
|
|
|
|
>>> it = iter('abcdef')
|
|
|
|
|
>>> take(3, it)
|
|
|
|
|
['a', 'b', 'c']
|
|
|
|
|
>>> list(it)
|
|
|
|
|
['d', 'e', 'f']
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> list(prepend(1, [2, 3, 4]))
|
|
|
|
|
[1, 2, 3, 4]
|
|
|
|
|
|
|
|
|
|
>>> list(enumerate('abc'))
|
|
|
|
|
[(0, 'a'), (1, 'b'), (2, 'c')]
|
|
|
|
|
|
|
|
|
|
>>> list(islice(tabulate(lambda x: 2*x), 4))
|
|
|
|
|
[0, 2, 4, 6]
|
|
|
|
|
|
|
|
|
|
>>> list(tail(3, 'ABCDEFG'))
|
|
|
|
|
['E', 'F', 'G']
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify the input is consumed greedily
|
|
|
|
|
>>> input_iterator = iter('ABCDEFG')
|
|
|
|
|
>>> output_iterator = tail(3, input_iterator)
|
|
|
|
|
>>> list(input_iterator)
|
|
|
|
|
[]
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> it = iter(range(10))
|
|
|
|
|
>>> consume(it, 3)
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify the input is consumed lazily
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> next(it)
|
|
|
|
|
3
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify the input is consumed completely
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> consume(it)
|
|
|
|
|
>>> next(it, 'Done')
|
|
|
|
|
'Done'
|
|
|
|
|
|
|
|
|
|
>>> nth('abcde', 3)
|
|
|
|
|
'd'
|
|
|
|
|
>>> nth('abcde', 9) is None
|
|
|
|
|
True
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify that the input is consumed lazily
|
|
|
|
|
>>> it = iter('abcde')
|
|
|
|
|
>>> nth(it, 2)
|
|
|
|
|
'c'
|
|
|
|
|
>>> list(it)
|
|
|
|
|
['d', 'e']
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> [all_equal(s) for s in ('', 'A', 'AAAA', 'AAAB', 'AAABA')]
|
|
|
|
|
[True, True, True, False, False]
|
2024-02-28 19:04:56 -04:00
|
|
|
|
>>> [all_equal(s, key=str.casefold) for s in ('', 'A', 'AaAa', 'AAAB', 'AAABA')]
|
|
|
|
|
[True, True, True, False, False]
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify that the input is consumed lazily and that only
|
|
|
|
|
>>> # one element of a second equivalence class is used to disprove
|
|
|
|
|
>>> # the assertion that all elements are equal.
|
|
|
|
|
>>> it = iter('aaabbbccc')
|
|
|
|
|
>>> all_equal(it)
|
|
|
|
|
False
|
|
|
|
|
>>> ''.join(it)
|
|
|
|
|
'bbccc'
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> quantify(range(99), lambda x: x%2==0)
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
|
>>> quantify([True, False, False, True, True])
|
|
|
|
|
3
|
|
|
|
|
|
2024-03-04 11:51:29 -04:00
|
|
|
|
>>> quantify(range(12), predicate=lambda x: x%2==1)
|
2022-01-25 08:56:53 -04:00
|
|
|
|
6
|
|
|
|
|
|
|
|
|
|
>>> a = [[1, 2, 3], [4, 5, 6]]
|
|
|
|
|
>>> list(flatten(a))
|
|
|
|
|
[1, 2, 3, 4, 5, 6]
|
|
|
|
|
|
|
|
|
|
>>> list(repeatfunc(pow, 5, 2, 3))
|
|
|
|
|
[8, 8, 8, 8, 8]
|
|
|
|
|
|
|
|
|
|
>>> take(5, map(int, repeatfunc(random.random)))
|
|
|
|
|
[0, 0, 0, 0, 0]
|
|
|
|
|
|
|
|
|
|
>>> list(ncycles('abc', 3))
|
|
|
|
|
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify greedy consumption of input iterator
|
|
|
|
|
>>> input_iterator = iter('abc')
|
|
|
|
|
>>> output_iterator = ncycles(input_iterator, 3)
|
|
|
|
|
>>> list(input_iterator)
|
|
|
|
|
[]
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
2023-01-13 21:01:22 -04:00
|
|
|
|
>>> sum_of_squares([10, 20, 30])
|
|
|
|
|
1400
|
|
|
|
|
|
2023-12-15 20:03:44 -04:00
|
|
|
|
>>> list(reshape([(0, 1), (2, 3), (4, 5)], 3))
|
|
|
|
|
[(0, 1, 2), (3, 4, 5)]
|
|
|
|
|
>>> M = [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)]
|
|
|
|
|
>>> list(reshape(M, 1))
|
|
|
|
|
[(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,), (10,), (11,)]
|
|
|
|
|
>>> list(reshape(M, 2))
|
|
|
|
|
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11)]
|
|
|
|
|
>>> list(reshape(M, 3))
|
|
|
|
|
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)]
|
|
|
|
|
>>> list(reshape(M, 4))
|
|
|
|
|
[(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11)]
|
2023-12-16 11:13:50 -04:00
|
|
|
|
>>> list(reshape(M, 5))
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
|
...
|
|
|
|
|
ValueError: batched(): incomplete batch
|
2023-12-15 20:03:44 -04:00
|
|
|
|
>>> list(reshape(M, 6))
|
|
|
|
|
[(0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11)]
|
|
|
|
|
>>> list(reshape(M, 12))
|
|
|
|
|
[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)]
|
|
|
|
|
|
2023-01-13 21:01:22 -04:00
|
|
|
|
>>> list(transpose([(1, 2, 3), (11, 22, 33)]))
|
|
|
|
|
[(1, 11), (2, 22), (3, 33)]
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify that the inputs are consumed lazily
|
|
|
|
|
>>> input1 = iter([1, 2, 3])
|
|
|
|
|
>>> input2 = iter([11, 22, 33])
|
|
|
|
|
>>> output_iterator = transpose([input1, input2])
|
|
|
|
|
>>> next(output_iterator)
|
|
|
|
|
(1, 11)
|
|
|
|
|
>>> list(zip(input1, input2))
|
|
|
|
|
[(2, 22), (3, 33)]
|
2023-01-13 21:01:22 -04:00
|
|
|
|
|
|
|
|
|
>>> list(matmul([(7, 5), (3, 5)], [[2, 5], [7, 9]]))
|
|
|
|
|
[(49, 80), (41, 60)]
|
|
|
|
|
>>> list(matmul([[2, 5], [7, 9], [3, 4]], [[7, 11, 5, 4, 9], [3, 5, 2, 6, 3]]))
|
|
|
|
|
[(29, 47, 20, 38, 33), (76, 122, 53, 82, 90), (33, 53, 23, 36, 39)]
|
|
|
|
|
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> list(convolve([1, -1, -20], [1, -3])) == [1, -4, -17, 60]
|
|
|
|
|
True
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> data = [20, 40, 24, 32, 20, 28, 16]
|
|
|
|
|
>>> list(convolve(data, [0.25, 0.25, 0.25, 0.25]))
|
|
|
|
|
[5.0, 15.0, 21.0, 29.0, 29.0, 26.0, 24.0, 16.0, 11.0, 4.0]
|
|
|
|
|
>>> list(convolve(data, [1, -1]))
|
|
|
|
|
[20, 20, -16, 8, -12, 8, -12, -16]
|
|
|
|
|
>>> list(convolve(data, [1, -2, 1]))
|
|
|
|
|
[20, 0, -36, 24, -20, 20, -20, -4, 16]
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify signal is consumed lazily and the kernel greedily
|
|
|
|
|
>>> signal_iterator = iter([10, 20, 30, 40, 50])
|
|
|
|
|
>>> kernel_iterator = iter([1, 2, 3])
|
|
|
|
|
>>> output_iterator = convolve(signal_iterator, kernel_iterator)
|
|
|
|
|
>>> list(kernel_iterator)
|
|
|
|
|
[]
|
|
|
|
|
>>> next(output_iterator)
|
|
|
|
|
10
|
|
|
|
|
>>> next(output_iterator)
|
|
|
|
|
40
|
|
|
|
|
>>> list(signal_iterator)
|
|
|
|
|
[30, 40, 50]
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
2023-03-20 19:14:29 -03:00
|
|
|
|
>>> from fractions import Fraction
|
|
|
|
|
>>> from decimal import Decimal
|
2024-03-13 17:02:56 -03:00
|
|
|
|
>>> polynomial_eval([1, -4, -17, 60], x=5)
|
|
|
|
|
0
|
|
|
|
|
>>> x = 5; x**3 - 4*x**2 -17*x + 60
|
|
|
|
|
0
|
2023-03-20 19:14:29 -03:00
|
|
|
|
>>> polynomial_eval([1, -4, -17, 60], x=2.5)
|
|
|
|
|
8.125
|
|
|
|
|
>>> x = 2.5; x**3 - 4*x**2 -17*x + 60
|
|
|
|
|
8.125
|
|
|
|
|
>>> polynomial_eval([1, -4, -17, 60], x=Fraction(2, 3))
|
|
|
|
|
Fraction(1274, 27)
|
|
|
|
|
>>> x = Fraction(2, 3); x**3 - 4*x**2 -17*x + 60
|
|
|
|
|
Fraction(1274, 27)
|
|
|
|
|
>>> polynomial_eval([1, -4, -17, 60], x=Decimal('1.75'))
|
|
|
|
|
Decimal('23.359375')
|
|
|
|
|
>>> x = Decimal('1.75'); x**3 - 4*x**2 -17*x + 60
|
|
|
|
|
Decimal('23.359375')
|
|
|
|
|
>>> polynomial_eval([], 2)
|
|
|
|
|
0
|
|
|
|
|
>>> polynomial_eval([], 2.5)
|
|
|
|
|
0.0
|
|
|
|
|
>>> polynomial_eval([], Fraction(2, 3))
|
|
|
|
|
Fraction(0, 1)
|
|
|
|
|
>>> polynomial_eval([], Decimal('1.75'))
|
2023-09-19 09:39:44 -03:00
|
|
|
|
Decimal('0')
|
2023-03-20 19:14:29 -03:00
|
|
|
|
>>> polynomial_eval([11], 7) == 11
|
|
|
|
|
True
|
|
|
|
|
>>> polynomial_eval([11, 2], 7) == 11 * 7 + 2
|
|
|
|
|
True
|
|
|
|
|
|
2022-08-21 11:41:29 -03:00
|
|
|
|
>>> polynomial_from_roots([5, -4, 3])
|
|
|
|
|
[1, -4, -17, 60]
|
|
|
|
|
>>> factored = lambda x: (x - 5) * (x + 4) * (x - 3)
|
|
|
|
|
>>> expanded = lambda x: x**3 -4*x**2 -17*x + 60
|
|
|
|
|
>>> all(factored(x) == expanded(x) for x in range(-10, 11))
|
|
|
|
|
True
|
|
|
|
|
|
2023-05-25 00:31:02 -03:00
|
|
|
|
>>> polynomial_derivative([1, -4, -17, 60])
|
|
|
|
|
[3, -8, -17]
|
|
|
|
|
|
2022-10-15 14:43:58 -03:00
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A'))
|
|
|
|
|
[0, 1, 4, 7]
|
|
|
|
|
>>> list(iter_index('AABCADEAF', 'B'))
|
|
|
|
|
[2]
|
|
|
|
|
>>> list(iter_index('AABCADEAF', 'X'))
|
|
|
|
|
[]
|
|
|
|
|
>>> list(iter_index('', 'X'))
|
|
|
|
|
[]
|
2022-10-18 16:09:34 -03:00
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A', 1))
|
|
|
|
|
[1, 4, 7]
|
|
|
|
|
>>> list(iter_index(iter('AABCADEAF'), 'A', 1))
|
|
|
|
|
[1, 4, 7]
|
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A', 2))
|
|
|
|
|
[4, 7]
|
|
|
|
|
>>> list(iter_index(iter('AABCADEAF'), 'A', 2))
|
|
|
|
|
[4, 7]
|
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A', 10))
|
|
|
|
|
[]
|
|
|
|
|
>>> list(iter_index(iter('AABCADEAF'), 'A', 10))
|
|
|
|
|
[]
|
2023-09-03 00:25:13 -03:00
|
|
|
|
>>> list(iter_index('AABCADEAF', 'A', 1, 7))
|
|
|
|
|
[1, 4]
|
|
|
|
|
>>> list(iter_index(iter('AABCADEAF'), 'A', 1, 7))
|
|
|
|
|
[1, 4]
|
|
|
|
|
>>> # Verify that ValueErrors not swallowed (gh-107208)
|
|
|
|
|
>>> def assert_no_value(iterable, forbidden_value):
|
|
|
|
|
... for item in iterable:
|
|
|
|
|
... if item == forbidden_value:
|
|
|
|
|
... raise ValueError
|
|
|
|
|
... yield item
|
|
|
|
|
...
|
|
|
|
|
>>> list(iter_index(assert_no_value('AABCADEAF', 'B'), 'A'))
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
|
...
|
|
|
|
|
ValueError
|
2023-09-11 23:04:28 -03:00
|
|
|
|
>>> # Verify that both paths can find identical NaN values
|
|
|
|
|
>>> x = float('NaN')
|
|
|
|
|
>>> y = float('NaN')
|
|
|
|
|
>>> list(iter_index([0, x, x, y, 0], x))
|
|
|
|
|
[1, 2]
|
|
|
|
|
>>> list(iter_index(iter([0, x, x, y, 0]), x))
|
|
|
|
|
[1, 2]
|
|
|
|
|
>>> # Test list input. Lists do not support None for the stop argument
|
|
|
|
|
>>> list(iter_index(list('AABCADEAF'), 'A'))
|
|
|
|
|
[0, 1, 4, 7]
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify that input is consumed lazily
|
|
|
|
|
>>> input_iterator = iter('AABCADEAF')
|
|
|
|
|
>>> output_iterator = iter_index(input_iterator, 'A')
|
|
|
|
|
>>> next(output_iterator)
|
|
|
|
|
0
|
|
|
|
|
>>> next(output_iterator)
|
|
|
|
|
1
|
|
|
|
|
>>> next(output_iterator)
|
|
|
|
|
4
|
|
|
|
|
>>> ''.join(input_iterator)
|
|
|
|
|
'DEAF'
|
2022-10-15 14:43:58 -03:00
|
|
|
|
|
2024-03-12 23:33:42 -03:00
|
|
|
|
>>> # Verify that the target value can be a sequence.
|
|
|
|
|
>>> seq = [[10, 20], [30, 40], 30, 40, [30, 40], 50]
|
|
|
|
|
>>> target = [30, 40]
|
|
|
|
|
>>> list(iter_index(seq, target))
|
|
|
|
|
[1, 4]
|
|
|
|
|
|
|
|
|
|
>>> # Verify faithfulness to type specific index() method behaviors.
|
2024-03-14 23:40:36 -03:00
|
|
|
|
>>> # For example, bytes and str perform continuous-subsequence searches
|
2024-03-12 23:33:42 -03:00
|
|
|
|
>>> # that do not match the general behavior specified
|
|
|
|
|
>>> # in collections.abc.Sequence.index().
|
|
|
|
|
>>> seq = 'abracadabra'
|
|
|
|
|
>>> target = 'ab'
|
|
|
|
|
>>> list(iter_index(seq, target))
|
|
|
|
|
[0, 7]
|
|
|
|
|
|
|
|
|
|
|
2022-09-13 23:10:47 -03:00
|
|
|
|
>>> list(sieve(30))
|
|
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
|
2022-10-07 05:37:21 -03:00
|
|
|
|
>>> small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
|
|
|
|
|
>>> all(list(sieve(n)) == [p for p in small_primes if p < n] for n in range(101))
|
2022-09-17 13:31:04 -03:00
|
|
|
|
True
|
2022-09-13 23:10:47 -03:00
|
|
|
|
>>> len(list(sieve(100)))
|
|
|
|
|
25
|
|
|
|
|
>>> len(list(sieve(1_000)))
|
|
|
|
|
168
|
|
|
|
|
>>> len(list(sieve(10_000)))
|
|
|
|
|
1229
|
|
|
|
|
>>> len(list(sieve(100_000)))
|
|
|
|
|
9592
|
|
|
|
|
>>> len(list(sieve(1_000_000)))
|
|
|
|
|
78498
|
2022-09-17 13:31:04 -03:00
|
|
|
|
>>> carmichael = {561, 1105, 1729, 2465, 2821, 6601, 8911} # https://oeis.org/A002997
|
|
|
|
|
>>> set(sieve(10_000)).isdisjoint(carmichael)
|
|
|
|
|
True
|
2022-09-13 23:10:47 -03:00
|
|
|
|
|
2023-07-16 23:37:07 -03:00
|
|
|
|
>>> list(factor(99)) # Code example 1
|
|
|
|
|
[3, 3, 11]
|
|
|
|
|
>>> list(factor(1_000_000_000_000_007)) # Code example 2
|
|
|
|
|
[47, 59, 360620266859]
|
|
|
|
|
>>> list(factor(1_000_000_000_000_403)) # Code example 3
|
|
|
|
|
[1000000000000403]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(0))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(1))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(2))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(3))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[3]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(4))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2, 2]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(5))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[5]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(6))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2, 3]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(7))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[7]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(8))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2, 2, 2]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(9))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[3, 3]
|
2022-12-28 07:13:58 -04:00
|
|
|
|
>>> list(factor(10))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
[2, 5]
|
2022-12-28 16:29:27 -04:00
|
|
|
|
>>> list(factor(128_884_753_939)) # large prime
|
|
|
|
|
[128884753939]
|
|
|
|
|
>>> list(factor(999953 * 999983)) # large semiprime
|
2022-12-28 07:13:58 -04:00
|
|
|
|
[999953, 999983]
|
2022-12-28 16:29:27 -04:00
|
|
|
|
>>> list(factor(6 ** 20)) == [2] * 20 + [3] * 20 # large power
|
|
|
|
|
True
|
|
|
|
|
>>> list(factor(909_909_090_909)) # large multiterm composite
|
|
|
|
|
[3, 3, 7, 13, 13, 751, 113797]
|
|
|
|
|
>>> math.prod([3, 3, 7, 13, 13, 751, 113797])
|
|
|
|
|
909909090909
|
|
|
|
|
>>> all(math.prod(factor(n)) == n for n in range(1, 2_000))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
True
|
2022-12-28 16:29:27 -04:00
|
|
|
|
>>> all(set(factor(n)) <= set(sieve(n+1)) for n in range(2_000))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
True
|
2022-12-28 16:29:27 -04:00
|
|
|
|
>>> all(list(factor(n)) == sorted(factor(n)) for n in range(2_000))
|
2022-12-24 04:21:30 -04:00
|
|
|
|
True
|
|
|
|
|
|
2023-12-14 15:15:29 -04:00
|
|
|
|
>>> totient(0) # https://www.wolframalpha.com/input?i=totient+0
|
|
|
|
|
0
|
|
|
|
|
>>> first_totients = [1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16, 6,
|
|
|
|
|
... 18, 8, 12, 10, 22, 8, 20, 12, 18, 12, 28, 8, 30, 16, 20, 16, 24, 12, 36, 18,
|
|
|
|
|
... 24, 16, 40, 12, 42, 20, 24, 22, 46, 16, 42, 20, 32, 24, 52, 18, 40, 24, 36,
|
|
|
|
|
... 28, 58, 16, 60, 30, 36, 32, 48, 20, 66, 32, 44] # https://oeis.org/A000010
|
|
|
|
|
...
|
|
|
|
|
>>> list(map(totient, range(1, 70))) == first_totients
|
|
|
|
|
True
|
|
|
|
|
>>> reference_totient = lambda n: sum(math.gcd(t, n) == 1 for t in range(1, n+1))
|
|
|
|
|
>>> all(totient(n) == reference_totient(n) for n in range(1000))
|
|
|
|
|
True
|
|
|
|
|
>>> totient(128_884_753_939) == 128_884_753_938 # large prime
|
|
|
|
|
True
|
|
|
|
|
>>> totient(999953 * 999983) == 999952 * 999982 # large semiprime
|
|
|
|
|
True
|
|
|
|
|
>>> totient(6 ** 20) == 1 * 2**19 * 2 * 3**19 # repeated primes
|
|
|
|
|
True
|
|
|
|
|
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')]))
|
|
|
|
|
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
|
|
|
|
|
|
|
|
|
|
>>> random.seed(85753098575309)
|
|
|
|
|
>>> list(repeatfunc(random.random, 3))
|
|
|
|
|
[0.16370491282496968, 0.45889608687313455, 0.3747076837820118]
|
|
|
|
|
>>> list(repeatfunc(chr, 3, 65))
|
|
|
|
|
['A', 'A', 'A']
|
|
|
|
|
>>> list(repeatfunc(pow, 3, 2, 5))
|
|
|
|
|
[32, 32, 32]
|
|
|
|
|
|
|
|
|
|
>>> list(grouper('abcdefg', 3, fillvalue='x'))
|
|
|
|
|
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'x', 'x')]
|
|
|
|
|
|
|
|
|
|
>>> it = grouper('abcdefg', 3, incomplete='strict')
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('a', 'b', 'c')
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('d', 'e', 'f')
|
|
|
|
|
>>> next(it)
|
|
|
|
|
Traceback (most recent call last):
|
|
|
|
|
...
|
|
|
|
|
ValueError: zip() argument 2 is shorter than argument 1
|
|
|
|
|
|
|
|
|
|
>>> list(grouper('abcdefg', n=3, incomplete='ignore'))
|
|
|
|
|
[('a', 'b', 'c'), ('d', 'e', 'f')]
|
|
|
|
|
|
2023-06-06 15:44:45 -03:00
|
|
|
|
>>> list(sliding_window('ABCDEFG', 1))
|
|
|
|
|
[('A',), ('B',), ('C',), ('D',), ('E',), ('F',), ('G',)]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 2))
|
|
|
|
|
[('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'F'), ('F', 'G')]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 3))
|
|
|
|
|
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> list(sliding_window('ABCDEFG', 4))
|
|
|
|
|
[('A', 'B', 'C', 'D'), ('B', 'C', 'D', 'E'), ('C', 'D', 'E', 'F'), ('D', 'E', 'F', 'G')]
|
2023-06-06 15:44:45 -03:00
|
|
|
|
>>> list(sliding_window('ABCDEFG', 5))
|
|
|
|
|
[('A', 'B', 'C', 'D', 'E'), ('B', 'C', 'D', 'E', 'F'), ('C', 'D', 'E', 'F', 'G')]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 6))
|
|
|
|
|
[('A', 'B', 'C', 'D', 'E', 'F'), ('B', 'C', 'D', 'E', 'F', 'G')]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 7))
|
|
|
|
|
[('A', 'B', 'C', 'D', 'E', 'F', 'G')]
|
|
|
|
|
>>> list(sliding_window('ABCDEFG', 8))
|
|
|
|
|
[]
|
|
|
|
|
>>> try:
|
|
|
|
|
... list(sliding_window('ABCDEFG', -1))
|
|
|
|
|
... except ValueError:
|
|
|
|
|
... 'zero or negative n not supported'
|
|
|
|
|
...
|
|
|
|
|
'zero or negative n not supported'
|
|
|
|
|
>>> try:
|
|
|
|
|
... list(sliding_window('ABCDEFG', 0))
|
|
|
|
|
... except ValueError:
|
|
|
|
|
... 'zero or negative n not supported'
|
|
|
|
|
...
|
|
|
|
|
'zero or negative n not supported'
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> list(roundrobin('abc', 'd', 'ef'))
|
|
|
|
|
['a', 'd', 'e', 'b', 'f', 'c']
|
2024-03-13 04:12:30 -03:00
|
|
|
|
>>> ranges = [range(5, 1000), range(4, 3000), range(0), range(3, 2000), range(2, 5000), range(1, 3500)]
|
2024-03-14 23:40:36 -03:00
|
|
|
|
>>> collections.Counter(roundrobin(*ranges)) == collections.Counter(chain(*ranges))
|
2024-03-13 04:12:30 -03:00
|
|
|
|
True
|
2024-03-14 18:39:50 -03:00
|
|
|
|
>>> # Verify that the inputs are consumed lazily
|
|
|
|
|
>>> input_iterators = list(map(iter, ['abcd', 'ef', '', 'ghijk', 'l', 'mnopqr']))
|
|
|
|
|
>>> output_iterator = roundrobin(*input_iterators)
|
|
|
|
|
>>> ''.join(islice(output_iterator, 10))
|
|
|
|
|
'aeglmbfhnc'
|
|
|
|
|
>>> ''.join(chain(*input_iterators))
|
|
|
|
|
'dijkopqr'
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> def is_odd(x):
|
|
|
|
|
... return x % 2 == 1
|
|
|
|
|
|
|
|
|
|
>>> evens, odds = partition(is_odd, range(10))
|
|
|
|
|
>>> list(evens)
|
|
|
|
|
[0, 2, 4, 6, 8]
|
|
|
|
|
>>> list(odds)
|
|
|
|
|
[1, 3, 5, 7, 9]
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify that the input is consumed lazily
|
|
|
|
|
>>> input_iterator = iter(range(10))
|
|
|
|
|
>>> evens, odds = partition(is_odd, input_iterator)
|
|
|
|
|
>>> next(odds)
|
|
|
|
|
1
|
|
|
|
|
>>> next(odds)
|
|
|
|
|
3
|
|
|
|
|
>>> next(evens)
|
|
|
|
|
0
|
|
|
|
|
>>> list(input_iterator)
|
|
|
|
|
[4, 5, 6, 7, 8, 9]
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
2022-02-02 00:18:52 -04:00
|
|
|
|
>>> list(subslices('ABCD'))
|
|
|
|
|
['A', 'AB', 'ABC', 'ABCD', 'B', 'BC', 'BCD', 'C', 'CD', 'D']
|
|
|
|
|
|
2022-01-25 08:56:53 -04:00
|
|
|
|
>>> list(powerset([1,2,3]))
|
|
|
|
|
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
|
|
|
|
|
|
|
|
|
|
>>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
>>> list(powerset('abcde')) == sorted(sorted(set(powerset('abcde'))), key=len)
|
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
>>> list(unique_everseen('AAAABBBCCDAABBB'))
|
|
|
|
|
['A', 'B', 'C', 'D']
|
2023-12-25 18:26:04 -04:00
|
|
|
|
>>> list(unique_everseen('ABBCcAD', str.casefold))
|
2022-01-25 08:56:53 -04:00
|
|
|
|
['A', 'B', 'C', 'D']
|
2023-12-25 18:26:04 -04:00
|
|
|
|
>>> list(unique_everseen('ABBcCAD', str.casefold))
|
2022-12-30 22:36:38 -04:00
|
|
|
|
['A', 'B', 'c', 'D']
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify that the input is consumed lazily
|
|
|
|
|
>>> input_iterator = iter('AAAABBBCCDAABBB')
|
|
|
|
|
>>> output_iterator = unique_everseen(input_iterator)
|
|
|
|
|
>>> next(output_iterator)
|
|
|
|
|
'A'
|
|
|
|
|
>>> ''.join(input_iterator)
|
|
|
|
|
'AAABBBCCDAABBB'
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> list(unique_justseen('AAAABBBCCDAABBB'))
|
|
|
|
|
['A', 'B', 'C', 'D', 'A', 'B']
|
2023-12-25 18:26:04 -04:00
|
|
|
|
>>> list(unique_justseen('ABBCcAD', str.casefold))
|
2022-01-25 08:56:53 -04:00
|
|
|
|
['A', 'B', 'C', 'A', 'D']
|
2023-12-25 18:26:04 -04:00
|
|
|
|
>>> list(unique_justseen('ABBcCAD', str.casefold))
|
2022-12-30 22:36:38 -04:00
|
|
|
|
['A', 'B', 'c', 'A', 'D']
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify that the input is consumed lazily
|
|
|
|
|
>>> input_iterator = iter('AAAABBBCCDAABBB')
|
|
|
|
|
>>> output_iterator = unique_justseen(input_iterator)
|
|
|
|
|
>>> next(output_iterator)
|
|
|
|
|
'A'
|
|
|
|
|
>>> ''.join(input_iterator)
|
|
|
|
|
'AAABBBCCDAABBB'
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
|
|
|
|
>>> d = dict(a=1, b=2, c=3)
|
|
|
|
|
>>> it = iter_except(d.popitem, KeyError)
|
|
|
|
|
>>> d['d'] = 4
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('d', 4)
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('c', 3)
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('b', 2)
|
|
|
|
|
>>> d['e'] = 5
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('e', 5)
|
|
|
|
|
>>> next(it)
|
|
|
|
|
('a', 1)
|
|
|
|
|
>>> next(it, 'empty')
|
|
|
|
|
'empty'
|
|
|
|
|
|
|
|
|
|
>>> first_true('ABC0DEF1', '9', str.isdigit)
|
|
|
|
|
'0'
|
2024-03-12 19:19:58 -03:00
|
|
|
|
>>> # Verify that inputs are consumed lazily
|
|
|
|
|
>>> it = iter('ABC0DEF1')
|
|
|
|
|
>>> first_true(it, predicate=str.isdigit)
|
|
|
|
|
'0'
|
|
|
|
|
>>> ''.join(it)
|
|
|
|
|
'DEF1'
|
2022-01-25 08:56:53 -04:00
|
|
|
|
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
|
|
|
|
.. testcode::
|
|
|
|
|
:hide:
|
|
|
|
|
|
|
|
|
|
# Old recipes and their tests which are guaranteed to continue to work.
|
|
|
|
|
|
|
|
|
|
def sumprod(vec1, vec2):
|
|
|
|
|
"Compute a sum of products."
|
|
|
|
|
return sum(starmap(operator.mul, zip(vec1, vec2, strict=True)))
|
|
|
|
|
|
|
|
|
|
def dotproduct(vec1, vec2):
|
|
|
|
|
return sum(map(operator.mul, vec1, vec2))
|
|
|
|
|
|
|
|
|
|
def pad_none(iterable):
|
|
|
|
|
"""Returns the sequence elements and then returns None indefinitely.
|
|
|
|
|
|
|
|
|
|
Useful for emulating the behavior of the built-in map() function.
|
|
|
|
|
"""
|
|
|
|
|
return chain(iterable, repeat(None))
|
|
|
|
|
|
|
|
|
|
def triplewise(iterable):
|
|
|
|
|
"Return overlapping triplets from an iterable"
|
2024-03-14 18:39:50 -03:00
|
|
|
|
# triplewise('ABCDEFG') → ABC BCD CDE DEF EFG
|
2023-04-09 16:17:37 -03:00
|
|
|
|
for (a, _), (b, c) in pairwise(pairwise(iterable)):
|
|
|
|
|
yield a, b, c
|
|
|
|
|
|
2023-12-14 16:36:40 -04:00
|
|
|
|
def nth_combination(iterable, r, index):
|
|
|
|
|
"Equivalent to list(combinations(iterable, r))[index]"
|
|
|
|
|
pool = tuple(iterable)
|
|
|
|
|
n = len(pool)
|
|
|
|
|
c = math.comb(n, r)
|
|
|
|
|
if index < 0:
|
|
|
|
|
index += c
|
|
|
|
|
if index < 0 or index >= c:
|
|
|
|
|
raise IndexError
|
|
|
|
|
result = []
|
|
|
|
|
while r:
|
|
|
|
|
c, n, r = c*r//n, n-1, r-1
|
|
|
|
|
while index >= c:
|
|
|
|
|
index -= c
|
|
|
|
|
c, n = c*(n-r)//n, n-1
|
|
|
|
|
result.append(pool[-1-n])
|
|
|
|
|
return tuple(result)
|
|
|
|
|
|
2024-02-25 08:32:14 -04:00
|
|
|
|
def before_and_after(predicate, it):
|
|
|
|
|
""" Variant of takewhile() that allows complete
|
|
|
|
|
access to the remainder of the iterator.
|
|
|
|
|
|
|
|
|
|
>>> it = iter('ABCdEfGhI')
|
|
|
|
|
>>> all_upper, remainder = before_and_after(str.isupper, it)
|
|
|
|
|
>>> ''.join(all_upper)
|
|
|
|
|
'ABC'
|
|
|
|
|
>>> ''.join(remainder) # takewhile() would lose the 'd'
|
|
|
|
|
'dEfGhI'
|
|
|
|
|
|
|
|
|
|
Note that the true iterator must be fully consumed
|
|
|
|
|
before the remainder iterator can generate valid results.
|
|
|
|
|
"""
|
|
|
|
|
it = iter(it)
|
|
|
|
|
transition = []
|
|
|
|
|
|
|
|
|
|
def true_iterator():
|
|
|
|
|
for elem in it:
|
|
|
|
|
if predicate(elem):
|
|
|
|
|
yield elem
|
|
|
|
|
else:
|
|
|
|
|
transition.append(elem)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
return true_iterator(), chain(transition, it)
|
2023-04-09 16:17:37 -03:00
|
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
:hide:
|
|
|
|
|
|
|
|
|
|
>>> dotproduct([1,2,3], [4,5,6])
|
|
|
|
|
32
|
|
|
|
|
|
|
|
|
|
>>> sumprod([1,2,3], [4,5,6])
|
|
|
|
|
32
|
|
|
|
|
|
|
|
|
|
>>> list(islice(pad_none('abc'), 0, 6))
|
|
|
|
|
['a', 'b', 'c', None, None, None]
|
|
|
|
|
|
|
|
|
|
>>> list(triplewise('ABCDEFG'))
|
|
|
|
|
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
|
2023-12-14 16:36:40 -04:00
|
|
|
|
|
|
|
|
|
>>> population = 'ABCDEFGH'
|
|
|
|
|
>>> for r in range(len(population) + 1):
|
|
|
|
|
... seq = list(combinations(population, r))
|
|
|
|
|
... for i in range(len(seq)):
|
|
|
|
|
... assert nth_combination(population, r, i) == seq[i]
|
|
|
|
|
... for i in range(-len(seq), 0):
|
|
|
|
|
... assert nth_combination(population, r, i) == seq[i]
|
|
|
|
|
|
|
|
|
|
>>> iterable = 'abcde'
|
|
|
|
|
>>> r = 3
|
|
|
|
|
>>> combos = list(combinations(iterable, r))
|
|
|
|
|
>>> all(nth_combination(iterable, r, i) == comb for i, comb in enumerate(combos))
|
|
|
|
|
True
|
2024-02-25 08:32:14 -04:00
|
|
|
|
|
|
|
|
|
>>> it = iter('ABCdEfGhI')
|
|
|
|
|
>>> all_upper, remainder = before_and_after(str.isupper, it)
|
|
|
|
|
>>> ''.join(all_upper)
|
|
|
|
|
'ABC'
|
|
|
|
|
>>> ''.join(remainder)
|
|
|
|
|
'dEfGhI'
|