8000 bpo-42450: Minor updates to the itertools recipes (GH-23555) (GH-23562) · python/cpython@a83119d · GitHub
[go: up one dir, main page]

Skip to content

Commit a83119d

Browse files
bpo-42450: Minor updates to the itertools recipes (GH-23555) (GH-23562)
1 parent fcf7391 commit a83119d

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

Doc/library/itertools.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ which incur interpreter overhead.
755755
"Count how many times the predicate is true"
756756
return sum(map(pred, iterable))
757757

758-
def padnone(iterable):
758+
def pad_none(iterable):
759759
"""Returns the sequence elements and then returns None indefinitely.
760760

761761
Useful for emulating the behavior of the built-in map() function.
@@ -809,7 +809,7 @@ which incur interpreter overhead.
809809
nexts = cycle(islice(nexts, num_active))
810810
811811
def partition(pred, iterable):
812-
'Use a predicate to partition entries into false entries and true entries'
812+
"Use a predicate to partition entries into false entries and true entries"
813813
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
814814
t1, t2 = tee(iterable)
815815
return filterfalse(pred, t1), filter(pred, t2)
@@ -881,7 +881,7 @@ which incur interpreter overhead.
881881
def random_product(*args, repeat=1):
882882
"Random selection from itertools.product(*args, **kwds)"
883883
pools = [tuple(pool) for pool in args] * repeat
884-
return tuple(random.choice(pool) for pool in pools)
884+
return tuple(map(random.choice, pools))
885885
886886
def random_permutation(iterable, r=None):
887887
"Random selection from itertools.permutations(iterable, r)"
@@ -900,11 +900,11 @@ which incur interpreter overhead.
900900
"Random selection from itertools.combinations_with_replacement(iterable, r)"
901901
pool = tuple(iterable)
902902
n = len(pool)
903-
indices = sorted(random.randrange(n) for i in range(r))
903+
indices = sorted(random.choices(range(n), k=r))
904904
return tuple(pool[i] for i in indices)
905905

906906
def nth_combination(iterable, r, index):
907-
'Equivalent to list(combinations(iterable, r))[index]'
907+
"Equivalent to list(combinations(iterable, r))[index]"
908908
pool = tuple(iterable)
909909
n = len(pool)
910910
if r < 0 or r > n:

Lib/test/test_itertools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,7 +2290,7 @@ def test_permutations_sizeof(self):
22902290
... "Count how many times the predicate is true"
22912291
... return sum(map(pred, iterable))
22922292
2293-
>>> def padnone(iterable):
2293+
>>> def pad_none(iterable):
22942294
... "Returns the sequence elements and then returns None indefinitely"
22952295
... return chain(iterable, repeat(None))
22962296
@@ -2460,7 +2460,7 @@ def test_permutations_sizeof(self):
24602460
>>> list(pairwise('a'))
24612461
[]
24622462
2463-
>>> list(islice(padnone('abc'), 0, 6))
2463+
>>> list(islice(pad_none('abc'), 0, 6))
24642464
['a', 'b', 'c', None, None, None]
24652465
24662466
>>> list(ncycles('abc', 3))

0 commit comments

Comments
 (0)
0