8000 Minor improvements to the itertools recipes (#118563) · python/cpython@6d9e8e9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d9e8e9

Browse files
authored
Minor improvements to the itertools recipes (#118563)
1 parent a40f557 commit 6d9e8e9

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

Doc/library/itertools.rst

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -826,10 +826,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
826826
return map(function, count(start))
827827

828828
def repeatfunc(func, times=None, *args):
829-
"""Repeat calls to func with specified arguments.
830-
831-
Example: repeatfunc(random.random)
832-
"""
829+
"Repeat calls to func with specified arguments."
833830
if times is None:
834831
return starmap(func, repeat(args))
835832
return starmap(func, repeat(args, times))
@@ -851,10 +848,8 @@ and :term:`generators <generator>` which incur interpreter overhead.
851848
"Advance the iterator n-steps ahead. If n is None, consume entirely."
852849
# Use functions that consume iterators at C speed.
853850
if n is None:
854-
# feed the entire iterator into a zero-length deque
855851
collections.deque(iterator, maxlen=0)
856852
else:
857-
# advance to the empty slice starting at position n
858853
next(islice(iterator, n, n), None)
859854

860855
def nth(iterable, n, default=None):
@@ -873,7 +868,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
873868

874869
def all_equal(iterable, key=None):
875870
"Returns True if all the elements are equal to each other."
876-
# all_equal('4٤໔4৪', key=int) → True
871+
# all_equal('4٤௪౪໔', key=int) → True
877872
return len(take(2, groupby(iterable, key))) <= 1
878873

879874
def unique_justseen(iterable, key=None):
@@ -903,9 +898,9 @@ and :term:`generators <generator>` which incur interpreter overhead.
903898
def sliding_window(iterable, n):
904899
"Collect data into overlapping fixed-length chunks or blocks."
905900
# sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG
906-
it = iter(iterable)
907-
window = collections.deque(islice(it, n-1), maxlen=n)
908-
for x in it:
901+
iterator = iter(iterable)
902+
window = collections.deque(islice(iterator, n - 1), maxlen=n)
903+
for x in iterator:
909904
window.append(x)
910905
yield tuple(window)
911906

@@ -955,8 +950,8 @@ and :term:`generators <generator>` which incur interpreter overhead.
955950
seq_index = getattr(iterable, 'index', None)
956951
if seq_index is None:
957952
# Path for general iterables
958-
it = islice(iterable, start, stop)
959-
for i, element in enumerate(it, start):
953+
iterator = islice(iterable, start, stop)
954+
for i, element in enumerate(iterator, start):
960955
if element is value or element == value:
961956
yield i
962957
else:
@@ -971,10 +966,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
971966
pass
972967

973968
def iter_except(func, exception, first=None):
974-
""" Call a function repeatedly until an exception is raised.
975-
976-
Converts a call-until-exception interface to an iterator interface.
977-
"""
969+
"Convert a call-until-exception interface to an iterator interface."
978970
# iter_except(d.popitem, KeyError) → non-blocking dictionary iterator
979971
try:
980972
if first is not None:
@@ -1074,14 +1066,10 @@ The following recipes have a more mathematical flavor:
10741066
# sieve(30) → 2 3 5 7 11 13 17 19 23 29
10751067
if n > 2:
10761068
yield 2
1077-
start = 3
10781069
data = bytearray((0, 1)) * (n // 2)
1079-
limit = math.isqrt(n) + 1
1080-
for p in iter_index(data, 1, start, limit):
1081-
yield from iter_index(data, 1, start, p*p)
1070+
for p in iter_index(data, 1, start=3, stop=math.isqrt(n) + 1):
10821071
data[p*p : n : p+p] = bytes(len(range(p*p, n, p+p)))
1083-
start = p*p
1084-
yield from iter_index(data, 1, start)
1072+
yield from iter_index(data, 1, start=3)
10851073

10861074
def factor(n):
10871075
"Prime factors of n."
@@ -1101,8 +1089,8 @@ The following recipes have a more mathematical flavor:
11011089
"Count of natural numbers up to n that are coprime to n."
11021090
# https://mathworld.wolfram.com/TotientFunction.html
11031091
# totient(12) → 4 because len([1, 5, 7, 11]) == 4
1104-
for p in unique_justseen(factor(n)):
1105-
n -= n // p
1092+
for prime in set(factor(n)):
1093+
n -= n // prime
11061094
return n
11071095

11081096

0 commit comments

Comments
 (0)
0