diff options
author | Raymond Hettinger <python@rcn.com> | 2009-01-26 02:17:52 +0000 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-01-26 02:17:52 +0000 |
commit | 5cd012b64e7a1139997ae09f9858fb73bd2c2ca3 (patch) | |
tree | 6c3a8a8cb3d84596e1de830f509fc3892441ffdd /Lib/test/test_itertools.py | |
parent | a96f9f673247429a3e1c9b7f76f90965bb8f5265 (diff) | |
download | cpython-git-5cd012b64e7a1139997ae09f9858fb73bd2c2ca3.tar.gz |
Backport r68942: update powerset() recipe.
Diffstat (limited to 'Lib/test/test_itertools.py')
-rw-r--r-- | Lib/test/test_itertools.py | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 2182cb963c..0f1f695a2d 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -1256,11 +1256,9 @@ Samuele ... 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" @@ -1358,8 +1356,8 @@ perform as purported. >>> list(roundrobin('abc', 'd', 'ef')) ['a', 'd', 'e', 'b', 'f', 'c'] ->>> 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'] |