mirror of
https://github.com/python/cpython.git
synced 2025-08-22 01:35:16 +00:00
Add and update itertools recipes.
This commit is contained in:
parent
11d36bc135
commit
ce2338a16b
1 changed files with 23 additions and 1 deletions
|
@ -669,7 +669,8 @@ which incur interpreter overhead.
|
||||||
return sum(imap(operator.mul, vec1, vec2))
|
return sum(imap(operator.mul, vec1, vec2))
|
||||||
|
|
||||||
def flatten(listOfLists):
|
def flatten(listOfLists):
|
||||||
return list(chain.from_iterable(listOfLists))
|
"Flatten one level of nesting"
|
||||||
|
return chain.from_iterable(listOfLists)
|
||||||
|
|
||||||
def repeatfunc(func, times=None, *args):
|
def repeatfunc(func, times=None, *args):
|
||||||
"""Repeat calls to func with specified arguments.
|
"""Repeat calls to func with specified arguments.
|
||||||
|
@ -778,6 +779,27 @@ which incur interpreter overhead.
|
||||||
except exception:
|
except exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
def random_permuation(iterable, r=None):
|
||||||
|
"Random selection from itertools.permutations(iterable, r)"
|
||||||
|
pool = list(iterable)
|
||||||
|
r = len(pool) if r is None else r
|
||||||
|
return 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)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
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
|
||||||
*dotproduct* recipe can be written as::
|
*dotproduct* recipe can be written as::
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue