[Python-checkins] r79573 - python/branches/py3k/Doc/library/itertools.rst

raymond.hettinger python-checkins at python.org
Fri Apr 2 06:50:35 CEST 2010

Author: raymond.hettinger
Date: Fri Apr  2 06:50:35 2010
New Revision: 79573

Fix nits in itertools recipes.


Modified: python/branches/py3k/Doc/library/itertools.rst
--- python/branches/py3k/Doc/library/itertools.rst	(original)
+++ python/branches/py3k/Doc/library/itertools.rst	Fri Apr  2 06:50:35 2010
@@ -611,7 +611,7 @@
    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 @@
    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

More information about the Python-checkins mailing list