[Python-checkins] r68959 - in python/branches/release30-maint: Doc/library/itertools.rst Lib/test/test_itertools.py
raymond.hettinger
python-checkins at python.org
Mon Jan 26 03:22:03 CET 2009
Author: raymond.hettinger
Date: Mon Jan 26 03:22:01 2009
New Revision: 68959
Log:
Backport r68942: update powerset() recipe.
Modified:
python/branches/release30-maint/Doc/library/itertools.rst
python/branches/release30-maint/Lib/test/test_itertools.py
Modified: python/branches/release30-maint/Doc/library/itertools.rst
==============================================================================
--- python/branches/release30-maint/Doc/library/itertools.rst (original)
+++ python/branches/release30-maint/Doc/library/itertools.rst Mon Jan 26 03:22:01 2009
@@ -590,11 +590,9 @@
nexts = cycle(islice(nexts, pending))
def powerset(iterable):
- "powerset('ab') --> set([]), set(['a']), set(['b']), set(['a', 'b'])"
- # Recipe credited to Eric Raymond
- pairs = [(2**i, x) for i, x in enumerate(iterable)]
- for n in xrange(2**len(pairs)):
- yield set(x for m, x in pairs if m&n)
+ "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
+ s = list(iterable)
+ return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
def compress(data, selectors):
"compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
Modified: python/branches/release30-maint/Lib/test/test_itertools.py
==============================================================================
--- python/branches/release30-maint/Lib/test/test_itertools.py (original)
+++ python/branches/release30-maint/Lib/test/test_itertools.py Mon Jan 26 03:22:01 2009
@@ -1277,11 +1277,9 @@
... nexts = cycle(islice(nexts, pending))
>>> def powerset(iterable):
-... "powerset('ab') --> set([]), set(['a']), set(['b']), set(['a', 'b'])"
-... # Recipe credited to Eric Raymond
-... pairs = [(2**i, x) for i, x in enumerate(iterable)]
-... for n in range(2**len(pairs)):
-... yield set(x for m, x in pairs if m&n)
+... "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
+... s = list(iterable)
+... return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
>>> def compress(data, selectors):
... "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
@@ -1379,8 +1377,8 @@
>>> list(roundrobin('abc', 'd', 'ef'))
['a', 'd', 'e', 'b', 'f', 'c']
->>> list(map(sorted, powerset('ab')))
-[[], ['a'], ['b'], ['a', 'b']]
+>>> list(powerset([1,2,3]))
+[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
>>> list(compress('abcdef', [1,0,1,0,1,1]))
['a', 'c', 'e', 'f']
More information about the Python-checkins
mailing list