bpo-42450: Minor updates to the itertools recipes (GH-23555)
This commit is contained in:
parent
86684319d3
commit
fc40b3020c
|
@ -755,7 +755,7 @@ which incur interpreter overhead.
|
||||||
"Count how many times the predicate is true"
|
"Count how many times the predicate is true"
|
||||||
return sum(map(pred, iterable))
|
return sum(map(pred, iterable))
|
||||||
|
|
||||||
def padnone(iterable):
|
def pad_none(iterable):
|
||||||
"""Returns the sequence elements and then returns None indefinitely.
|
"""Returns the sequence elements and then returns None indefinitely.
|
||||||
|
|
||||||
Useful for emulating the behavior of the built-in map() function.
|
Useful for emulating the behavior of the built-in map() function.
|
||||||
|
@ -809,7 +809,7 @@ which incur interpreter overhead.
|
||||||
nexts = cycle(islice(nexts, num_active))
|
nexts = cycle(islice(nexts, num_active))
|
||||||
|
|
||||||
def partition(pred, iterable):
|
def partition(pred, iterable):
|
||||||
'Use a predicate to partition entries into false entries and true entries'
|
"Use a predicate to partition entries into false entries and true entries"
|
||||||
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
|
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
|
||||||
t1, t2 = tee(iterable)
|
t1, t2 = tee(iterable)
|
||||||
return filterfalse(pred, t1), filter(pred, t2)
|
return filterfalse(pred, t1), filter(pred, t2)
|
||||||
|
@ -881,7 +881,7 @@ which incur interpreter overhead.
|
||||||
def random_product(*args, repeat=1):
|
def random_product(*args, repeat=1):
|
||||||
"Random selection from itertools.product(*args, **kwds)"
|
"Random selection from itertools.product(*args, **kwds)"
|
||||||
pools = [tuple(pool) for pool in args] * repeat
|
pools = [tuple(pool) for pool in args] * repeat
|
||||||
return tuple(random.choice(pool) for pool in pools)
|
return tuple(map(random.choice, pools))
|
||||||
|
|
||||||
def random_permutation(iterable, r=None):
|
def random_permutation(iterable, r=None):
|
||||||
"Random selection from itertools.permutations(iterable, r)"
|
"Random selection from itertools.permutations(iterable, r)"
|
||||||
|
@ -900,11 +900,11 @@ which incur interpreter overhead.
|
||||||
"Random selection from itertools.combinations_with_replacement(iterable, r)"
|
"Random selection from itertools.combinations_with_replacement(iterable, r)"
|
||||||
pool = tuple(iterable)
|
pool = tuple(iterable)
|
||||||
n = len(pool)
|
n = len(pool)
|
||||||
indices = sorted(random.randrange(n) for i in range(r))
|
indices = sorted(random.choices(range(n), k=r))
|
||||||
return tuple(pool[i] for i in indices)
|
return tuple(pool[i] for i in indices)
|
||||||
|
|
||||||
def nth_combination(iterable, r, index):
|
def nth_combination(iterable, r, index):
|
||||||
'Equivalent to list(combinations(iterable, r))[index]'
|
"Equivalent to list(combinations(iterable, r))[index]"
|
||||||
pool = tuple(iterable)
|
pool = tuple(iterable)
|
||||||
n = len(pool)
|
n = len(pool)
|
||||||
if r < 0 or r > n:
|
if r < 0 or r > n:
|
||||||
|
|
|
@ -2290,7 +2290,7 @@ Samuele
|
||||||
... "Count how many times the predicate is true"
|
... "Count how many times the predicate is true"
|
||||||
... return sum(map(pred, iterable))
|
... return sum(map(pred, iterable))
|
||||||
|
|
||||||
>>> def padnone(iterable):
|
>>> def pad_none(iterable):
|
||||||
... "Returns the sequence elements and then returns None indefinitely"
|
... "Returns the sequence elements and then returns None indefinitely"
|
||||||
... return chain(iterable, repeat(None))
|
... return chain(iterable, repeat(None))
|
||||||
|
|
||||||
|
@ -2460,7 +2460,7 @@ True
|
||||||
>>> list(pairwise('a'))
|
>>> list(pairwise('a'))
|
||||||
[]
|
[]
|
||||||
|
|
||||||
>>> list(islice(padnone('abc'), 0, 6))
|
>>> list(islice(pad_none('abc'), 0, 6))
|
||||||
['a', 'b', 'c', None, None, None]
|
['a', 'b', 'c', None, None, None]
|
||||||
|
|
||||||
>>> list(ncycles('abc', 3))
|
>>> list(ncycles('abc', 3))
|
||||||
|
|
Loading…
Reference in New Issue