Commit b640bcc3 authored by Raymond Hettinger's avatar Raymond Hettinger

Cleanup itertools recipes

parent ce2338a1
...@@ -663,7 +663,7 @@ which incur interpreter overhead. ...@@ -663,7 +663,7 @@ which incur interpreter overhead.
def ncycles(iterable, n): def ncycles(iterable, n):
"Returns the sequence elements n times" "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): def dotproduct(vec1, vec2):
return sum(imap(operator.mul, vec1, vec2)) return sum(imap(operator.mul, vec1, vec2))
...@@ -782,23 +782,23 @@ which incur interpreter overhead. ...@@ -782,23 +782,23 @@ which incur interpreter overhead.
def random_product(*args, **kwds): def random_product(*args, **kwds):
"Random selection from itertools.product(*args, **kwds)" "Random selection from itertools.product(*args, **kwds)"
pools = map(tuple, args) * kwds.get('repeat', 1) 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): def random_permuation(iterable, r=None):
"Random selection from itertools.permutations(iterable, r)" "Random selection from itertools.permutations(iterable, r)"
pool = list(iterable) pool = tuple(iterable)
r = len(pool) if r is None else r 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): def random_combination(iterable, r):
"Random selection from itertools.combinations(iterable, r)" "Random selection from itertools.combinations(iterable, r)"
pool = list(iterable) 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): def random_combination_with_replacement(iterable, r):
"Random selection from itertools.combinations_with_replacement(iterable, r)" "Random selection from itertools.combinations_with_replacement(iterable, r)"
pool = list(iterable) pool = tuple(iterable)
return sorted(imap(random.choice, [pool]*r), key=pool.index) return tuple(sorted(imap(random.choice, [pool]*r), key=pool.index))
Note, many of the above recipes can be optimized by replacing global lookups Note, many of the above recipes can be optimized by replacing global lookups
with local variables defined as default values. For example, the with local variables defined as default values. For example, the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment