diff options
author | Raymond Hettinger <python@rcn.com> | 2009-01-26 02:23:50 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-01-26 02:23:50 (GMT) |
commit | ace673391acbe0411b20b972b6068dab7a0bbdb4 (patch) | |
tree | b6483acf551bb28b1fd230ff0cd1188048c0cc52 /Doc/library/itertools.rst | |
parent | 89e12963ad7f0551b325b282267ad21011aa5fd7 (diff) | |
download | cpython-ace673391acbe0411b20b972b6068dab7a0bbdb4.zip cpython-ace673391acbe0411b20b972b6068dab7a0bbdb4.tar.gz cpython-ace673391acbe0411b20b972b6068dab7a0bbdb4.tar.bz2 |
Backport r68942: update powerset() recipe.
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r-- | Doc/library/itertools.rst | 8 |
1 files changed, 3 insertions, 5 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index db10b6d..36254cd 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -590,11 +590,9 @@ which incur interpreter overhead. 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" |