[Python-checkins] r79921 - python/branches/release31-maint/Doc/library/itertools.rst

raymond.hettinger python-checkins at python.org
Sat Apr 10 09:08:41 CEST 2010


Author: raymond.hettinger
Date: Sat Apr 10 09:08:41 2010
New Revision: 79921

Log:
Fixup new itertools recipes.

Modified:
   python/branches/release31-maint/Doc/library/itertools.rst

Modified: python/branches/release31-maint/Doc/library/itertools.rst
==============================================================================
--- python/branches/release31-maint/Doc/library/itertools.rst	(original)
+++ python/branches/release31-maint/Doc/library/itertools.rst	Sat Apr 10 09:08:41 2010
@@ -709,7 +709,7 @@
        pools = [tuple(pool) for pool in args] * repeat
        return tuple(random.choice(pool) for pool in pools)
 
-   def random_permuation(iterable, r=None):
+   def random_permutation(iterable, r=None):
        "Random selection from itertools.permutations(iterable, r)"
        pool = tuple(iterable)
        r = len(pool) if r is None else r
@@ -718,12 +718,16 @@
    def random_combination(iterable, r):
        "Random selection from itertools.combinations(iterable, r)"
        pool = tuple(iterable)
-       return tuple(sorted(random.sample(pool, r), key=pool.index))
+       n = len(pool)
+       indices = sorted(random.sample(range(n), r))
+       return tuple(pool[i] for i in indices)
 
    def random_combination_with_replacement(iterable, r):
        "Random selection from itertools.combinations_with_replacement(iterable, r)"
        pool = tuple(iterable)
-       return tuple(sorted(map(random.choice, repeat(pool, r)), key=pool.index))
+       n = len(pool)
+       indices = sorted(random.randrange(n) for i in range(r))
+       return tuple(pool[i] for i in indices)
 
 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