diff options
author | Raymond Hettinger <python@rcn.com> | 2010-04-02 04:50:35 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-04-02 04:50:35 (GMT) |
commit | 0f3ec6dd6255795d86ffb539866f58427f226280 (patch) | |
tree | 3be776932c3b6d8fcd4dcc4954235a9c5773e4c9 /Doc/library/itertools.rst | |
parent | 063a4b6880ad7e2fe33be702e33f948c346a72c6 (diff) | |
download | cpython-0f3ec6dd6255795d86ffb539866f58427f226280.zip cpython-0f3ec6dd6255795d86ffb539866f58427f226280.tar.gz cpython-0f3ec6dd6255795d86ffb539866f58427f226280.tar.bz2 |
Fix nits in itertools recipes.
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r-- | Doc/library/itertools.rst | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index bb1208c..5c63b12 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -611,7 +611,7 @@ which incur interpreter overhead. def ncycles(iterable, n): "Returns the sequence elements n times" - return chain.from_iterable(repeat(iterable, n)) + return chain.from_iterable(repeat(tuple(iterable), n)) def dotproduct(vec1, vec2): return sum(map(operator.mul, vec1, vec2)) @@ -707,23 +707,23 @@ which incur interpreter overhead. def random_product(*args, repeat=1): "Random selection from itertools.product(*args, **kwds)" pools = [tuple(pool) for pool in args] * repeat - return [random.choice(pool) for pool in pools] + return tuple(random.choice(pool) for pool in pools) def random_permuation(iterable, r=None): "Random selection from itertools.permutations(iterable, r)" pool = tuple(iterable) r = len(pool) if r is None else r - return random.sample(pool, r) + return tuple(random.sample(pool, r)) def random_combination(iterable, r): "Random selection from itertools.combinations(iterable, r)" pool = tuple(iterable) - return sorted(random.sample(pool, r), key=pool.index) + return tuple(sorted(random.sample(pool, r), key=pool.index)) def random_combination_with_replacement(iterable, r): "Random selection from itertools.combinations_with_replacement(iterable, r)" pool = tuple(iterable) - return sorted(map(random.choice, repeat(pool, r)), key=pool.index) + return tuple(sorted(map(random.choice, repeat(pool, r)), key=pool.index)) Note, many of the above recipes can be optimized by replacing global lookups with local variables defined as default values. For example, the |