diff options
author | Raymond Hettinger <python@rcn.com> | 2010-04-02 06:23:12 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-04-02 06:23:12 (GMT) |
commit | f28dd0d1bfd85e5285894bfac54f6668d7217210 (patch) | |
tree | 3dbec3d0189ef31f73680bc422ac9138a660138c /Doc | |
parent | 4bfd3bda2ea7e71219fffe8b3e36c818b590be16 (diff) | |
download | cpython-f28dd0d1bfd85e5285894bfac54f6668d7217210.zip cpython-f28dd0d1bfd85e5285894bfac54f6668d7217210.tar.gz cpython-f28dd0d1bfd85e5285894bfac54f6668d7217210.tar.bz2 |
Cleanup itertools recipes
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/itertools.rst | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 663b93c..c914b53 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -697,7 +697,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(imap(operator.mul, vec1, vec2)) @@ -794,23 +794,23 @@ which incur interpreter overhead. def random_product(*args, **kwds): "Random selection from itertools.product(*args, **kwds)" pools = map(tuple, args) * kwds.get('repeat', 1) - return map(random.choice, pools) + return tuple(random.choice(pool) for pool in pools) def random_permuation(iterable, r=None): "Random selection from itertools.permutations(iterable, r)" - pool = list(iterable) + 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 = list(iterable) - return sorted(random.sample(pool, r), key=pool.index) + pool = tuple(iterable) + 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 = list(iterable) - return sorted(imap(random.choice, [pool]*r), key=pool.index) + pool = tuple(iterable) + return tuple(sorted(imap(random.choice, [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 |