Files
ServerSync/lib/more_itertools/__pycache__/recipes.cpython-314.pyc

773 lines
50 KiB
Plaintext
Raw Normal View History

2026-02-19 00:55:27 +02:00
+
<00>;<3B>iS<69><00><00>h<00>Rt^RIt^RIHtHt^RIHt^RIHt^RI H
t
H t H t ^RI HtHt^RIHtHtHtHtHtHtHtHtHtHtHtHtHtHt^RIH t H!t!H"t"H#t#^R I$H%t%H&t&H't'H(t(H)t)^R
IH*t*H+t+H,t,^R I-H.t..R NR NRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNRNR NR!NR"NR#NR$NR%NR&NR'NR(NR)NR*NR+NR,NR-NR.NR/NR0NR1NR2NR3NR4NR5NR6NR7NR8NR9NR:NR;NR<NR=Nt/]0!4t1]2!R>R?7] !]2R>R?7t3^R@IH5t6^RBI H8t8H9t9R>t:RDt;R<>RElt<RFt=R<>RGlt>R<>RHlt?R<>RIlt@]A3RJltBRKtC]CtDRLtERMtFRNtGR<47>ROltHRPtI^RQIHJtKRRtJ]IP]Jn!RSRT]L4tMRUtNRVtOR<4F>RWltPRXtQRYtRRZtSR<53>R[ltTR<54>R\ltUR<55>R]ltVR<56>R^ltWR<57>R_ltXR`^/RaltYR<59>RbltZRct[Rdt\Ret]Rft^Rgt_Rht`RitaRjtbRktcRltdRmteRntfR<66>RoltgRpthRqRC/Rrlti].Rs8<73>d ^RtIHjtkRqRC/Rultj]iP]jnM]itjRvtl]m]n33RwltoRxtpRytqRztrR{ts]t!]h!^<5E>44tuR|tvR}twR~txRtyR<79>tz.R<>Ot{]
R<EFBFBD>4t|R<>t}]P<>!4PTtR<7F>t<>R<EFBFBD>t<>R<EFBFBD>t<>R<EFBFBD>t<>R<EFBFBD>t<>R<EFBFBD>t<>R<EFBFBD>R/R<>lt<>R# ]4d]2t3EL<>i;i ]7dRAt6EL<>i;i ]7dRCt:EL<>i;i ]7d]ItJELUi;i)<29>aImported from the recipes section of the itertools documentation.
All functions taken from the recipes section of the itertools library docs
[1]_.
Some backward-compatible usability improvements have been made.
.. [1] http://docs.python.org/library/itertools.html#recipes
N)<02> bisect_left<66>insort)<01>deque<75><01>suppress)<03> lru_cache<68>partial<61>reduce)<02>heappush<73> heappushpop)<0E>
accumulate<EFBFBD>chain<69> combinations<6E>compress<73>count<6E>cycle<6C>groupby<62>islice<63>product<63>repeat<61>starmap<61> takewhile<6C>tee<65> zip_longest)<04>prod<6F>comb<6D>isqrt<72>gcd)<05>mul<75>not_<74>
itemgetter<EFBFBD>getitem<65>index)<03> randrange<67>sample<6C>choice)<01>
hexversion<EFBFBD> all_equal<61>batched<65>before_and_after<65>consume<6D>convolve<76>
dotproduct<EFBFBD>
first_true<EFBFBD>factor<6F>flatten<65>grouper<65>is_prime<6D> iter_except<70>
iter_index<EFBFBD>loops<70>matmul<75> multinomial<61>ncycles<65>nth<74>nth_combination<6F>padnone<6E>pad_none<6E>pairwise<73> partition<6F>polynomial_eval<61>polynomial_from_roots<74>polynomial_derivative<76>powerset<65>prepend<6E>quantify<66>reshape<70>#random_combination_with_replacement<6E>random_combination<6F>random_permutation<6F>random_product<63>
repeatfunc<EFBFBD>
roundrobin<EFBFBD>running_median<61>sieve<76>sliding_window<6F> subslices<65>sum_of_squares<65>tabulate<74>tail<69>take<6B>totient<6E> transpose<73>
triplewise<EFBFBD>unique<75>unique_everseen<65>unique_justseenT<6E><01>strict)<01>sumprodc<00><00>\W4#<00>N)r,)<02>x<>ys&&<26>=/tmp/pip-target-ugtna5l2/lib/python/more_itertools/recipes.py<70><lambda>rals <00><00>J<EFBFBD>q<EFBFBD>,<2C>)<02> heappush_max<61>heappushpop_maxFc<04>*<00>\\W44#)z<>Return first *n* items of the *iterable* as a list.
>>> take(3, range(10))
[0, 1, 2]
If there are fewer than *n* items in the iterable, all of them are
returned.
>>> take(10, range(3))
[0, 1, 2]
)<02>listr)<02>n<>iterables&&r`rRrRxs<00><00> <10><06>x<EFBFBD>#<23> $<24>$rbc<04>,<00>\V\V44#)a<>Return an iterator over the results of ``func(start)``,
``func(start + 1)``, ``func(start + 2)``...
*func* should be a function that accepts one integer argument.
If *start* is not specified it defaults to 0. It will be incremented each
time the iterator is advanced.
>>> square = lambda x: x ** 2
>>> iterator = tabulate(square, -3)
>>> take(4, iterator)
[9, 4, 1, 0]
)<02>mapr)<02>function<6F>starts&&r`rPrP<00>s<00><00> <0F>x<EFBFBD><15>u<EFBFBD><1C> &<26>&rbc<04><><00>\V4p\V\^W ,
4R4# \d\ \ YR74u#i;i)zsReturn an iterator over the last *n* items of *iterable*.
>>> t = tail(3, 'ABCDEFG')
>>> list(t)
['E', 'F', 'G']
N<EFBFBD><01>maxlen)<06>lenr<00>max<61> TypeError<6F>iterr)rgrh<00>sizes&& r`rQrQ<00>sK<00><00>8<><12>8<EFBFBD>}<7D><04><16>h<EFBFBD><03>A<EFBFBD>t<EFBFBD>x<EFBFBD> 0<>$<24>7<>7<><37> <15>/<2F><13>E<EFBFBD>(<28>-<2D>.<2E>.<2E>/<2F>s<00> *<00> A <03> A c<04>X<00>Vf\V^R7R#\\WV4R4R#)a<>Advance *iterable* by *n* steps. If *n* is ``None``, consume it
entirely.
Efficiently exhausts an iterator without returning values. Defaults to
consuming the whole iterator, but an optional second argument may be
provided to limit consumption.
>>> i = (x for x in range(10))
>>> next(i)
0
>>> consume(i, 3)
>>> next(i)
4
>>> consume(i)
>>> next(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
If the iterator has fewer items remaining than the provided limit, the
whole iterator will be consumed.
>>> i = (x for x in range(3))
>>> consume(i, 5)
>>> next(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Nrn)r<00>nextr)<02>iteratorrgs&&r`r*r*<00>s'<00><00>@ <09>y<EFBFBD> <0A>h<EFBFBD>q<EFBFBD>!<21> <0A>V<EFBFBD>H<EFBFBD><11> #<23>T<EFBFBD>*rbc<04>.<00>\\WR4V4#)zmReturns the nth item or a default value.
>>> l = range(10)
>>> nth(l, 3)
3
>>> nth(l, 20, "zebra")
'zebra'
N)rvr)rhrg<00>defaults&&&r`r8r8<00>s<00><00> <10><06>x<EFBFBD>D<EFBFBD>)<29>7<EFBFBD> 3<>3rbc<04>F<00>\W4pVFpVFpR# R# R#)aw
Returns ``True`` if all the elements are equal to each other.
>>> all_equal('aaaa')
True
>>> all_equal('aaab')
False
A function that accepts a single argument and returns a transformed version
of each input item can be specified with *key*:
>>> all_equal('AaaA', key=str.casefold)
True
>>> all_equal([1, 2, 3], key=lambda x: x < 10)
True
FT)r)rh<00>keyrw<00>first<73>seconds&& r`r'r'<00>s-<00><00>$<17>x<EFBFBD>%<25>H<EFBFBD><19><05><1E>F<EFBFBD><18><1F><13><1A> rbc<04>*<00>\\W44#)zWReturn the how many times the predicate is true.
>>> quantify([True, False, True])
2
)<02>sumrj)rh<00>preds&&r`rCrC<00>s<00><00> <0F>s<EFBFBD>4<EFBFBD>"<22> #<23>#rbc<04>,<00>\V\R44#)z<>Returns the sequence of elements and then returns ``None`` indefinitely.
>>> take(5, pad_none(range(3)))
[0, 1, 2, None, None]
Useful for emulating the behavior of the built-in :func:`map` function.
See also :func:`padded`.
N)r r<00>rhs&r`r;r;s<00><00> <11><18>6<EFBFBD>$<24><<3C> (<28>(rbc<04>T<00>\P!\\V4V44#)zjReturns the sequence elements *n* times
>>> list(ncycles(["a", "b"], 3))
['a', 'b', 'a', 'b', 'a', 'b']
)r <00> from_iterabler<00>tuple<6C>rhrgs&&r`r7r7s <00><00> <11> <1E> <1E>v<EFBFBD>e<EFBFBD>H<EFBFBD>o<EFBFBD>q<EFBFBD>9<> :<3A>:rbc<04>4<00>\\\W44#)z<>Returns the dot product of the two iterables.
>>> dotproduct([10, 15, 12], [0.65, 0.80, 1.25])
33.5
>>> 10 * 0.65 + 15 * 0.80 + 12 * 1.25
33.5
In Python 3.12 and later, use ``math.sumprod()`` instead.
)rrjr)<02>vec1<63>vec2s&&r`r,r,s<00><00> <0F>s<EFBFBD>3<EFBFBD><04>#<23> $<24>$rbc<04>.<00>\P!V4#)z<>Return an iterator flattening one level of nesting in a list of lists.
>>> list(flatten([[0, 1], [2, 3]]))
[0, 1, 2, 3]
See also :func:`collapse`, which can flatten multiple levels of nesting.
)r r<>)<01> listOfListss&r`r/r/+s<00><00> <11> <1E> <1E>{<7B> +<2B>+rbc<04>^<00>Vf\V\V44#\V\W!44#)a Call *func* with *args* repeatedly, returning an iterable over the
results.
If *times* is specified, the iterable will terminate after that many
repetitions:
>>> from operator import add
>>> times = 4
>>> args = 3, 5
>>> list(repeatfunc(add, times, *args))
[8, 8, 8, 8]
If *times* is ``None`` the iterable will not terminate:
>>> from random import randrange
>>> times = None
>>> args = 1, 11
>>> take(6, repeatfunc(randrange, times, *args)) # doctest:+SKIP
[2, 4, 8, 1, 8, 4]
)rr)<03>func<6E>times<65>argss&&*r`rIrI7s,<00><00>, <0A>}<7D><16>t<EFBFBD>V<EFBFBD>D<EFBFBD>\<5C>*<2A>*<2A> <12>4<EFBFBD><16><04>,<2C> -<2D>-rbc<04>J<00>\V4wr\VR4\W4#)z<>Returns an iterator of paired items, overlapping, from the original
>>> take(4, pairwise(count()))
[(0, 1), (1, 2), (2, 3), (3, 4)]
On Python 3.10 and above, this is an alias for :func:`itertools.pairwise`.
N<EFBFBD>rrv<00>zip)rh<00>a<>bs& r`<00> _pairwiser<65>Rs"<00><00> <0F>x<EFBFBD>=<3D>D<EFBFBD>A<EFBFBD><08><11>D<EFBFBD>M<EFBFBD> <0E>q<EFBFBD>9<EFBFBD>rb)r<c<00><00>\V4#r])<01>itertools_pairwiser<65>s&r`r<r<fs <00><00>!<21>(<28>+<2B>+rbc<00>6aa<01>]tRtRtoRV3RlltRtVtV;t#)<05>UnequalIterablesErrorilc<08>`<<01>RpVeVRP!V!, p\SV` V4R#)z Iterables have different lengthsNz/: index 0 has length {}; index {} has length {})<03>format<61>super<65>__init__)<04>self<6C>details<6C>msg<73> __class__s&& <20>r`r<><00>UnequalIterablesError.__init__ms8<00><><00>0<><03> <12> <1E> <0F>E<>M<>M<><18><0E> <0E>C<EFBFBD> <0E><07><18><13>rb<00>r])<08>__name__<5F>
__module__<EFBFBD> __qualname__<5F>__firstlineno__r<5F><00>__static_attributes__<5F>__classdictcell__<5F> __classcell__)r<><00> __classdict__s@@r`r<>r<>ls<00><><00><00><1E>rbr<>c#<00>|"<00>\VR\/F$pVFpV\JgK\4h Vx<00>K& R#5i)<02> fillvalueN)r<00>_markerr<72>)<03> iterables<65>combo<62>vals& r`<00>_zip_equal_generatorr<72>ws:<00><00><00><1C>i<EFBFBD>;<3B>7<EFBFBD>;<3B><05><18>C<EFBFBD><12>g<EFBFBD>~<7E>+<2B>-<2D>-<2D><19><14> <0B> <<3C>s<00><<01><c<00><><00>\V^,4p\VR,^4F$wr#\V4pWA8wgK\WV3R7h \V!# \d\ T4u#i;i)<03>:<3A>NN)r<>)rp<00> enumerater<65>r<>rrr<>)r<><00>
first_size<EFBFBD>i<>itrts* r`<00>
_zip_equalr<EFBFBD>ss<00><00> /<2F><18><19>1<EFBFBD><1C>&<26>
<EFBFBD><1E>y<EFBFBD><12>}<7D>a<EFBFBD>0<>E<EFBFBD>A<EFBFBD><16>r<EFBFBD>7<EFBFBD>D<EFBFBD><13>!<21>+<2B>Z<EFBFBD>D<EFBFBD>4I<34>J<>J<>1<>
<13>I<EFBFBD><EFBFBD><1E><> <15>/<2F>#<23>I<EFBFBD>.<2E>.<2E>/<2F>s<00>8A<00>A<00>A1<03>0A1c<04><><00>\V4.V,pVR8Xd \VRV/#VR8Xd
\V!#VR8Xd
\V!#\ R4h)a<>Group elements from *iterable* into fixed-length groups of length *n*.
>>> list(grouper('ABCDEF', 3))
[('A', 'B', 'C'), ('D', 'E', 'F')]
The keyword arguments *incomplete* and *fillvalue* control what happens for
iterables whose length is not a multiple of *n*.
When *incomplete* is `'fill'`, the last group will contain instances of
*fillvalue*.
>>> list(grouper('ABCDEFG', 3, incomplete='fill', fillvalue='x'))
[('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'x', 'x')]
When *incomplete* is `'ignore'`, the last group will not be emitted.
>>> list(grouper('ABCDEFG', 3, incomplete='ignore', fillvalue='x'))
[('A', 'B', 'C'), ('D', 'E', 'F')]
When *incomplete* is `'strict'`, a subclass of `ValueError` will be raised.
>>> iterator = grouper('ABCDEFG', 3, incomplete='strict')
>>> list(iterator) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
UnequalIterablesError
<EFBFBD>fillr<6C>rZ<00>ignorez Expected fill, strict, or ignore)rsrr<>r<><00>
ValueError)rhrg<00>
incompleter<EFBFBD><00> iteratorss&&&& r`r0r0<00>s^<00><00>:<16>h<EFBFBD><1E> <20>1<EFBFBD>$<24>I<EFBFBD><11>V<EFBFBD><1B><1A>I<EFBFBD>;<3B><19>;<3B>;<3B><11>X<EFBFBD><1D><19>9<EFBFBD>%<25>%<25><11>X<EFBFBD><1D><12>I<EFBFBD><EFBFBD><1E><18>;<3B><<3C><rbc'<04><>"<00>\\V4p\\V4^R4F/p\ \ W44p\\ V4Rjx<01>L
K1 R#L
5i)a/Visit input iterables in a cycle until each is exhausted.
>>> list(roundrobin('ABC', 'D', 'EF'))
['A', 'D', 'E', 'B', 'F', 'C']
This function produces the same output as :func:`interleave_longest`, but
may perform better for some inputs (in particular when the number of
iterables is small).
N<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)rjrs<00>rangerprrrv)r<>r<><00>
num_actives* r`rJrJ<00>sL<00><00><00><14>D<EFBFBD>)<29>$<24>I<EFBFBD><1B>C<EFBFBD> <09>N<EFBFBD>A<EFBFBD>r<EFBFBD>2<>
<EFBFBD><19>&<26><19>7<>8<> <09><16>t<EFBFBD>Y<EFBFBD>'<27>'<27>'<27>3<>'<27>s<00>AA <01>A<06> A c<04><><00>Vf\p\V^4wr#p\\W44wrV\V\\V44\W643#)aw
Returns a 2-tuple of iterables derived from the input iterable.
The first yields the items that have ``pred(item) == False``.
The second yields the items that have ``pred(item) == True``.
>>> is_odd = lambda x: x % 2 != 0
>>> iterable = range(10)
>>> even_items, odd_items = partition(is_odd, iterable)
>>> list(even_items), list(odd_items)
([0, 2, 4, 6, 8], [1, 3, 5, 7, 9])
If *pred* is None, :func:`bool` is used.
>>> iterable = [0, 1, False, True, '', ' ']
>>> false_items, true_items = partition(None, iterable)
>>> list(false_items), list(true_items)
([0, False, ''], [1, True, ' '])
)<05>boolrrjrr)r<>rh<00>t1<74>t2<74>p<>p1<70>p2s&& r`r=r=<00>sK<00><00>( <0C>|<7C><13><04><13>H<EFBFBD>a<EFBFBD> <20>I<EFBFBD>B<EFBFBD>A<EFBFBD> <10><13>T<EFBFBD><1C> <1E>F<EFBFBD>B<EFBFBD> <14>R<EFBFBD><13>T<EFBFBD>2<EFBFBD><1D> '<27><18>"<22>)9<> :<3A>:rbc<04><>a<01>\V4o\P!V3Rl\\ S4^,444#)aYields all possible subsets of the iterable.
>>> list(powerset([1, 2, 3]))
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
:func:`powerset` will operate on iterables that aren't :class:`set`
instances, so repeated elements in the input will produce repeated elements
in the output.
>>> seq = [1, 1, 0]
>>> list(powerset(seq))
[(), (1,), (1,), (0,), (1, 1), (1, 0), (1, 0), (1, 1, 0)]
For a variant that efficiently yields actual :class:`set` instances, see
:func:`powerset_of_sets`.
c3<00><<"<00>TFp\SV4x<00>K R#5ir])r)<03>.0<EFBFBD>r<>ss& <20>r`<00> <genexpr><3E>powerset.<locals>.<genexpr><3E>s<00><><00><00>M<>;L<>a<EFBFBD>|<7C>A<EFBFBD>q<EFBFBD>1<>1<>;L<><4C><00>)rfr r<>r<>rp)rhr<>s&@r`rArA<00>s2<00><><00>" <0A>X<EFBFBD><0E>A<EFBFBD> <10> <1E> <1E>M<>5<EFBFBD><13>Q<EFBFBD><16>!<21><1A>;L<>M<> M<>Mrbc#<04> "<00>\4pVPp.pVPpVRJpVF*pV'd V!V4MTpW<>9dV!V4Vx<00>K*K, R# \dY<>9dT!T4Tx<00>KPKSi;i5i)aJ
Yield unique elements, preserving order.
>>> list(unique_everseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D']
>>> list(unique_everseen('ABBCcAD', str.lower))
['A', 'B', 'C', 'D']
Sequences with a mix of hashable and unhashable items can be used.
The function will be slower (i.e., `O(n^2)`) for unhashable items.
Remember that ``list`` objects are unhashable - you can use the *key*
parameter to transform the list to a tuple (which is hashable) to
avoid a slowdown.
>>> iterable = ([1, 2], [2, 3], [1, 2])
>>> list(unique_everseen(iterable)) # Slow
[[1, 2], [2, 3]]
>>> list(unique_everseen(iterable, key=tuple)) # Faster
[[1, 2], [2, 3]]
Similarly, you may want to convert unhashable ``set`` objects with
``key=frozenset``. For ``dict`` objects,
``key=lambda x: frozenset(x.items())`` can be used.
N)<04>set<65>add<64>appendrr) rhr{<00>seenset<65> seenset_add<64>seenlist<73> seenlist_add<64>use_key<65>element<6E>ks && r`rWrW<00>s<><00><00><00>6<12>e<EFBFBD>G<EFBFBD><19>+<2B>+<2B>K<EFBFBD><11>H<EFBFBD><1B>?<3F>?<3F>L<EFBFBD><11><14>o<EFBFBD>G<EFBFBD><1B><07>#<23>C<EFBFBD><07>L<EFBFBD><17><01> <1E><10><1F><1B>A<EFBFBD><0E><1D> <0A> <20><1C><> <19> <1E><10> <20><1C>Q<EFBFBD><0F><1D> <0A>!<21> <1E>s*<00>AB<01>A<02>B<01>B<05>:B<01>B<05>Bc
<04><><00>Vf\\^4\V44#\\\\^4\W444#)z<>Yields elements in order, ignoring serial duplicates
>>> list(unique_justseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D', 'A', 'B']
>>> list(unique_justseen('ABBCcAD', str.lower))
['A', 'B', 'C', 'A', 'D']
)rjr rrv)rhr{s&&r`rXrX's<<00><00> <0B>{<7B><12>:<3A>a<EFBFBD>=<3D>'<27>(<28>"3<>4<>4<> <0E>t<EFBFBD>S<EFBFBD><1A>A<EFBFBD><1D><07><08>(><3E>?<3F> @<40>@rbc<04>4<00>\WVR7p\W1R7#)a<>Yields unique elements in sorted order.
>>> list(unique([[1, 2], [3, 4], [1, 2]]))
[[1, 2], [3, 4]]
*key* and *reverse* are passed to :func:`sorted`.
>>> list(unique('ABBcCAD', str.casefold))
['A', 'B', 'c', 'D']
>>> list(unique('ABBcCAD', str.casefold, reverse=True))
['D', 'c', 'B', 'A']
The elements in *iterable* need not be hashable, but they must be
comparable for sorting to work.
)r{<00>reverse)r{)<02>sortedrX)rhr{r<><00> sequenceds&&& r`rVrV6s<00><00> <17>x<EFBFBD>'<27>:<3A>I<EFBFBD> <1A>9<EFBFBD> .<2E>.rbc#<04><>"<00>\V4;_uu_4Ve
V!4x<00>V!4x<00>K +'giR#;i5i)a<>Yields results from a function repeatedly until an exception is raised.
Converts a call-until-exception interface to an iterator interface.
Like ``iter(func, sentinel)``, but uses an exception instead of a sentinel
to end the loop.
>>> l = [0, 1, 2]
>>> list(iter_except(l.pop, IndexError))
[2, 1, 0]
Multiple exceptions can be specified as a stopping condition:
>>> l = [1, 2, 3, '...', 4, 5, 6]
>>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
[7, 6, 5]
>>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
[4, 3, 2]
>>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
[]
Nr)r<><00> exceptionr|s&&&r`r2r2Js4<00><00><00>,
<12>)<29> <1C> <1C> <10> <1C><17>'<27>M<EFBFBD><12><16>&<26>L<EFBFBD>
<1D> <1C> <1C>s<00>A<01>0<05> A <09> Ac<04>,<00>\\W 4V4#)ad
Returns the first true value in the iterable.
If no true value is found, returns *default*
If *pred* is not None, returns the first item for which
``pred(item) == True`` .
>>> first_true(range(10))
1
>>> first_true(range(10), pred=lambda x: x > 5)
6
>>> first_true(range(10), default='missing', pred=lambda x: x > 9)
'missing'
)rv<00>filter)rhryr<>s&&&r`r-r-gs<00><00>" <10><06>t<EFBFBD>&<26><07> 0<>0rbrc<04><><00>VUu.uFp\V4NK upV,p\;QJd.RV4FNK 5#!RV44#uupi)a<>Draw an item at random from each of the input iterables.
>>> random_product('abc', range(4), 'XYZ') # doctest:+SKIP
('c', 3, 'Z')
If *repeat* is provided as a keyword argument, that many items will be
drawn from each iterable.
>>> random_product('abcd', range(4), repeat=2) # doctest:+SKIP
('a', 2, 'd', 3)
This equivalent to taking a random selection from
``itertools.product(*args, repeat=repeat)``.
c3<00>8"<00>TFp\V4x<00>K R#5ir])r%)r<><00>pools& r`r<><00>!random_product.<locals>.<genexpr><3E>s<00><00><00>0<>%<25>$<24><16><04><1C><1C>%<25>s<00>)r<>)rr<>r<><00>poolss$* r`rHrH{sH<00><00> &*<2A> *<2A>T<EFBFBD>T<EFBFBD>U<EFBFBD>4<EFBFBD>[<5B>T<EFBFBD> *<2A>V<EFBFBD> 3<>E<EFBFBD> <10>5<EFBFBD>0<>%<25>0<>5<EFBFBD>0<>5<EFBFBD>0<>%<25>0<> 0<>0<><30> +s<00>Ac<04>b<00>\V4pVf \V4MTp\\W!44#)aFReturn a random *r* length permutation of the elements in *iterable*.
If *r* is not specified or is ``None``, then *r* defaults to the length of
*iterable*.
>>> random_permutation(range(5)) # doctest:+SKIP
(3, 4, 0, 1, 2)
This equivalent to taking a random selection from
``itertools.permutations(iterable, r)``.
)r<>rpr$)rhr<>r<>s&& r`rGrG<00>s+<00><00> <11><18>?<3F>D<EFBFBD><16>Y<EFBFBD><03>D<EFBFBD> <09>A<EFBFBD>A<EFBFBD> <10><16><04><1F> !<21>!rbc<04><>a<04>\V4o\S4p\\\ V4V44p\;QJd.V3RlV4FNK 5#!V3RlV44#)z<>Return a random *r* length subsequence of the elements in *iterable*.
>>> random_combination(range(5), 3) # doctest:+SKIP
(2, 3, 4)
This equivalent to taking a random selection from
``itertools.combinations(iterable, r)``.
c3<00>6<"<00>TFpSV,x<00>K R#5ir]r<><00>r<>r<>r<>s& <20>r`r<><00>%random_combination.<locals>.<genexpr><3E><00><00><><00><00>*<2A>'<27>Q<EFBFBD><14>a<EFBFBD><17><17>'<27><><00>)r<>rpr<>r$r<>)rhr<>rg<00>indicesr<73>s&& @r`rFrF<00>sN<00><><00> <11><18>?<3F>D<EFBFBD> <0B>D<EFBFBD> <09>A<EFBFBD><14>V<EFBFBD>E<EFBFBD>!<21>H<EFBFBD>a<EFBFBD>(<28>)<29>G<EFBFBD> <10>5<EFBFBD>*<2A>'<27>*<2A>5<EFBFBD>*<2A>5<EFBFBD>*<2A>'<27>*<2A> *<2A>*rbc<04><>aa<04>\V4o\S4o\V3Rl\V444p\;QJd.V3RlV4FNK 5#!V3RlV44#)a;Return a random *r* length subsequence of elements in *iterable*,
allowing individual elements to be repeated.
>>> random_combination_with_replacement(range(3), 5) # doctest:+SKIP
(0, 0, 1, 2, 2)
This equivalent to taking a random selection from
``itertools.combinations_with_replacement(iterable, r)``.
c3<00>:<"<00>TFp\S4x<00>K R#5ir])r#<00>r<>r<>rgs& <20>r`r<><00>6random_combination_with_replacement.<locals>.<genexpr><3E>s<00><><00><00>4<>8<EFBFBD>a<EFBFBD>Y<EFBFBD>q<EFBFBD>\<5C>\<5C>8<EFBFBD>s<00>c3<00>6<"<00>TFpSV,x<00>K R#5ir]r<>r<>s& <20>r`r<>r<><00>r<>r<>)r<>rpr<>r<>)rhr<>r<>rgr<>s&& @@r`rErE<00>sM<00><><00> <11><18>?<3F>D<EFBFBD> <0B>D<EFBFBD> <09>A<EFBFBD><14>4<>5<EFBFBD><11>8<EFBFBD>4<>4<>G<EFBFBD> <10>5<EFBFBD>*<2A>'<27>*<2A>5<EFBFBD>*<2A>5<EFBFBD>*<2A>'<27>*<2A> *<2A>*rbc<04>6<00>\V4p\V4pV^8gW8<>d\h^p\WV,
4p\ ^V^,4F pWTV,
V,,V,pK" V^8d W%, pV^8gW%8<>d\
h.pV'dpWQ,V,V^,
V^,
rpW%8<>d)W%,pWTV,
,V,V^,
rEK.VP VRV,
,4Kw\V4#)a<>Equivalent to ``list(combinations(iterable, r))[index]``.
The subsequences of *iterable* that are of length *r* can be ordered
lexicographically. :func:`nth_combination` computes the subsequence at
sort position *index* directly, without computing the previous
subsequences.
>>> nth_combination(range(5), 3, 5)
(0, 3, 4)
``ValueError`` will be raised If *r* is negative or greater than the length
of *iterable*.
``IndexError`` will be raised if the given *index* is invalid.
r<EFBFBD>)r<>rpr<><00>minr<6E><00>
IndexErrorr<EFBFBD>) rhr<>r"r<>rg<00>cr<63>r<><00>results &&& r`r9r9<00>s<><00><00> <11><18>?<3F>D<EFBFBD> <0B>D<EFBFBD> <09>A<EFBFBD> <09>A<EFBFBD><05>1<EFBFBD>5<EFBFBD><18><18> <09>A<EFBFBD> <0B>A<EFBFBD>1<EFBFBD>u<EFBFBD> <0A>A<EFBFBD> <12>1<EFBFBD>a<EFBFBD>!<21>e<EFBFBD>_<EFBFBD><01> <0A>Q<EFBFBD><15><11><19>O<EFBFBD>q<EFBFBD> <20><01><1D> <0A>q<EFBFBD>y<EFBFBD> <0A>
<EFBFBD><05> <0A><01> <09>u<EFBFBD>z<EFBFBD><18><18> <0F>F<EFBFBD>
<0B><13>%<25>1<EFBFBD>*<2A>a<EFBFBD>!<21>e<EFBFBD>Q<EFBFBD><11>U<EFBFBD>a<EFBFBD><01><13>j<EFBFBD> <11>J<EFBFBD>E<EFBFBD><14>A<EFBFBD><05>;<3B>!<21>#<23>Q<EFBFBD><11>U<EFBFBD>q<EFBFBD><0E> <0A> <0A>d<EFBFBD>2<EFBFBD><01>6<EFBFBD>l<EFBFBD>#<23> <10><16>=<3D>rbc<04><00>\V.V4#)aYield *value*, followed by the elements in *iterator*.
>>> value = '0'
>>> iterator = ['1', '2', '3']
>>> list(prepend(value, iterator))
['0', '1', '2', '3']
To prepend multiple values, see :func:`itertools.chain`
or :func:`value_chain`.
)r )<02>valuerws&&r`rBrB<00>s<00><00> <11>%<25><17>(<28> #<23>#rbc#<04><>"<00>\V4RRR1,p\V4p\^.VR7V,p\V\ ^V^,
44F!pVP V4\ W4x<00>K# R#5i)u-Discrete linear convolution of two iterables.
Equivalent to polynomial multiplication.
For example, multiplying ``(x² -x - 20)`` by ``(x - 3)``
gives ``(x³ -4x² -17x + 60)``.
>>> list(convolve([1, -1, -20], [1, -3]))
[1, -4, -17, 60]
Examples of popular kinds of kernels:
* The kernel ``[0.25, 0.25, 0.25, 0.25]`` computes a moving average.
For image data, this blurs the image and reduces noise.
* The kernel ``[1/2, 0, -1/2]`` estimates the first derivative of
a function evaluated at evenly spaced inputs.
* The kernel ``[1, -2, 1]`` estimates the second derivative of a
function evaluated at evenly spaced inputs.
Convolutions are mathematically commutative; however, the inputs are
evaluated differently. The signal is consumed lazily and can be
infinite. The kernel is fully consumed before the calculations begin.
Supports all numeric types: int, float, complex, Decimal, Fraction.
References:
* Article: https://betterexplained.com/articles/intuitive-convolution/
* Video by 3Blue1Brown: https://www.youtube.com/watch?v=KuXjwB4LzSA
Nrnr<>)r<>rprr rr<><00>_sumprod)<05>signal<61>kernelrg<00>windowr^s&& r`r+r+<00>si<00><00><00>F<13>6<EFBFBD>]<5D>4<EFBFBD>R<EFBFBD>4<EFBFBD> <20>F<EFBFBD> <0B>F<EFBFBD> <0B>A<EFBFBD> <12>A<EFBFBD>3<EFBFBD>q<EFBFBD> !<21>A<EFBFBD> %<25>F<EFBFBD> <12>6<EFBFBD>6<EFBFBD>!<21>Q<EFBFBD><11>U<EFBFBD>+<2B> ,<2C><01><0E> <0A> <0A>a<EFBFBD><18><16>v<EFBFBD>&<26>&<26>-<2D>s<00>A:A<c<04>^<00>\V4wr#\\W4\V44pW#3#)a<>A variant of :func:`takewhile` that allows complete access to the
remainder of the iterator.
>>> it = iter('ABCdEfGhI')
>>> all_upper, remainder = before_and_after(str.isupper, it)
>>> ''.join(all_upper)
'ABC'
>>> ''.join(remainder) # takewhile() would lose the 'd'
'dEfGhI'
Note that the first iterator must be fully consumed before the second
iterator can generate valid results.
)rrrr<>)<04> predicater<65><00>trues<65>afters&& r`r)r)&s,<00><00><17>r<EFBFBD>7<EFBFBD>L<EFBFBD>E<EFBFBD> <14>Y<EFBFBD>y<EFBFBD>0<>#<23>e<EFBFBD>*<2A> =<3D>E<EFBFBD> <10><<3C>rbc<04><><00>\V^4wrp\VR4\VR4\VR4\WV4#)z<>Return overlapping triplets from *iterable*.
>>> list(triplewise('ABCDE'))
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E')]
Nr<EFBFBD>)rhr<>r<><00>t3s& r`rUrU9s;<00><00><15>X<EFBFBD>q<EFBFBD>!<21>J<EFBFBD>B<EFBFBD>B<EFBFBD><08><12>T<EFBFBD>N<EFBFBD><08><12>T<EFBFBD>N<EFBFBD><08><12>T<EFBFBD>N<EFBFBD> <0E>r<EFBFBD>r<EFBFBD>?<3F>rbc<00>~<00>\W4p\V4Fwr4\\WCV4R4K \ V!#r])rr<>rvrr<>)rhrgr<>r<>rws&& r`<00>_sliding_window_islicer Is8<00><00><13>H<EFBFBD> <20>I<EFBFBD> <20><19>+<2B> <0B><01> <0C>V<EFBFBD>H<EFBFBD><11> #<23>T<EFBFBD>*<2A>,<2C> <0E> <09>?<3F>rbc#<00><>"<00>\V4p\\W!^,
4VR7pVF!pVPV4\ V4x<00>K# R#5i)r<>rnN)rsrrr<>r<>)rhrgrwrr^s&& r`<00>_sliding_window_dequerQsC<00><00><00><13>H<EFBFBD>~<7E>H<EFBFBD> <12>6<EFBFBD>(<28><01>E<EFBFBD>*<2A>1<EFBFBD> 5<>F<EFBFBD> <15><01><0E> <0A> <0A>a<EFBFBD><18><13>F<EFBFBD>m<EFBFBD><1B><16>s<00>AAc<04><><00>V^8<>d \W4#V^8<>d \W4#V^8Xd \V4#V^8Xd \V4#\ RV 24h)a=Return a sliding window of width *n* over *iterable*.
>>> list(sliding_window(range(6), 4))
[(0, 1, 2, 3), (1, 2, 3, 4), (2, 3, 4, 5)]
If *iterable* has fewer than *n* items, then nothing is yielded:
>>> list(sliding_window(range(3), 4))
[]
For a variant with more features, see :func:`windowed`.
zn should be at least one, not )rr r<r<>r<>r<>s&&r`rMrMZs^<00><00> <09>2<EFBFBD>v<EFBFBD>$<24>X<EFBFBD>1<>1<>
<EFBFBD>Q<EFBFBD><15>%<25>h<EFBFBD>2<>2<>
<EFBFBD>a<EFBFBD><16><17><08>!<21>!<21>
<EFBFBD>a<EFBFBD><16><12>8<EFBFBD>}<7D><1C><18>9<>!<21><13>=<3D>><3E>>rbc
<04><><00>\V4p\\\\ \ V4^,4^44p\ \\V4V4#)z<>Return all contiguous non-empty subslices of *iterable*.
>>> list(subslices('ABC'))
[['A'], ['A', 'B'], ['A', 'B', 'C'], ['B'], ['B', 'C'], ['C']]
This is similar to :func:`substrings`, but emits items in a different
order.
) rfr<00>slicerr<>rprjr!r)rh<00>seq<65>slicess& r`rNrNss@<00><00> <0F>x<EFBFBD>.<2E>C<EFBFBD> <14>U<EFBFBD>L<EFBFBD><15>s<EFBFBD>3<EFBFBD>x<EFBFBD>!<21>|<7C>)<<3C>a<EFBFBD>@<40> A<>F<EFBFBD> <0E>w<EFBFBD><06>s<EFBFBD> <0B>V<EFBFBD> ,<2C>,rbc<04>N<00>^.pVFp\\V^V)344pK V#)uSCompute a polynomial's coefficients from its roots.
>>> roots = [5, -4, 3] # (x - 5) * (x + 4) * (x - 3)
>>> polynomial_from_roots(roots) # x³ - 4 x² - 17 x + 60
[1, -4, -17, 60]
Note that polynomial coefficients are specified in descending power order.
Supports all numeric types: int, float, complex, Decimal, Fraction.
)rfr+)<03>roots<74>poly<6C>roots& r`r?r?<00>s1<00><00> <0E>3<EFBFBD>D<EFBFBD><15><04><13>H<EFBFBD>T<EFBFBD>A<EFBFBD><04>u<EFBFBD>:<3A>.<2E>/<2F><04><16> <0F>Krbc#<04>L"<00>\VRR4pVf4\WV4p\WR4FwrgWqJg Wq8XgKVx<00>K R#Vf \V4MTpV^,
p\ \
4;_uu_4V!W^,V4;px<00>K +'giR#;i5i)a<>Yield the index of each place in *iterable* that *value* occurs,
beginning with index *start* and ending before index *stop*.
>>> list(iter_index('AABCADEAF', 'A'))
[0, 1, 4, 7]
>>> list(iter_index('AABCADEAF', 'A', 1)) # start index is inclusive
[1, 4, 7]
>>> list(iter_index('AABCADEAF', 'A', 1, 7)) # stop index is not inclusive
[1, 4]
The behavior for non-scalar *values* matches the built-in Python types.
>>> list(iter_index('ABCDABCD', 'AB'))
[0, 4]
>>> list(iter_index([0, 1, 2, 3, 0, 1, 2, 3], [0, 1]))
[]
>>> list(iter_index([[0, 1], [2, 3], [0, 1], [2, 3]], [0, 1]))
[0, 2]
See :func:`locate` for a more general means of finding the indexes
associated with particular values.
r"N)<06>getattrrr<>rprr<>)rhrrl<00>stop<6F> seq_indexrwr<>r<>s&&&& r`r3r3<00>s<><00><00><00>2<18><08>'<27>4<EFBFBD>0<>I<EFBFBD><10><18><19>(<28>4<EFBFBD>0<><08>#<23>H<EFBFBD>4<>J<EFBFBD>A<EFBFBD><16><1F>7<EFBFBD>#3<><17><07>5<>
!%<25> <0C>s<EFBFBD>8<EFBFBD>}<7D>$<24><04> <11>A<EFBFBD>I<EFBFBD><01> <15>j<EFBFBD> !<21> !<21><16>%<25>e<EFBFBD><11>U<EFBFBD>D<EFBFBD>9<>9<>q<EFBFBD>:<3A>"<22> !<21> !<21>s<00>6B$<01>;B$<01>8B<05> B! <09> B$c #<04><>"<00>V^8<>d^x<00>^p\R4V^,,p\V^V\V4^,R7Fdp\V^WV,4Rjx<01>L
\\ \ W3,WV,444W#V,WV,1&W3,pKf \V^V4Rjx<01>L
R#LdL5i)zYYield the primes less than n.
>>> list(sieve(30))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
)rN)r<>r<>)<06> bytearrayr3r<00>bytesrpr<>)rgrl<00>datar<61>s& r`rLrL<00>s<><00><00><00> <09>1<EFBFBD>u<EFBFBD><0F><07> <0A>E<EFBFBD> <14>V<EFBFBD> <1C><01>Q<EFBFBD><06> '<27>D<EFBFBD> <17><04>a<EFBFBD><15>U<EFBFBD>1<EFBFBD>X<EFBFBD><01>\<5C> :<3A><01><1D>d<EFBFBD>A<EFBFBD>u<EFBFBD>!<21>e<EFBFBD>4<>4<>4<>"'<27><03>E<EFBFBD>!<21>%<25><11><01>E<EFBFBD>,B<>(C<>"D<><04><11>U<EFBFBD>Q<EFBFBD>Q<EFBFBD><15> <1E><1F><11><05><05>;<3B><1A>$<24><01>5<EFBFBD>)<29>)<29>)<29> 5<>*<2A>s%<00>A C <01>"C<06>#AC <01>C <04>C <01> C rZc#<04><>"<00>V^8d \R4h\V4p\\W144;p'd*V'd\ V4V8wd \R4hVx<00>KER#5i)arBatch data into tuples of length *n*. If the number of items in
*iterable* is not divisible by *n*:
* The last batch will be shorter if *strict* is ``False``.
* :exc:`ValueError` will be raised if *strict* is ``True``.
>>> list(batched('ABCDEFG', 3))
[('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]
On Python 3.13 and above, this is an alias for :func:`itertools.batched`.
zn must be at least onezbatched(): incomplete batchN)r<>rsr<>rrp)rhrgrZrw<00>batchs&&$ r`<00>_batchedr#<00>s[<00><00><00> <09>1<EFBFBD>u<EFBFBD><18>1<>2<>2<><13>H<EFBFBD>~<7E>H<EFBFBD><18><16><08>,<2C>-<2D>
-<2D>%<25>
-<2D> <11>c<EFBFBD>%<25>j<EFBFBD>A<EFBFBD>o<EFBFBD><1C>:<3A>;<3B> ;<3B><13> <0B> .<2E>s <00>AA'<01>#A'i<> )r(c<00><00>\WVR7#)rY)<01>itertools_batched)rhrgrZs&&$r`r(r(<00>s<00><00> <20><18>V<EFBFBD><<3C><rbc<04><00>\V!#)z<>Swap the rows and columns of the input matrix.
>>> list(transpose([(1, 2, 3), (11, 22, 33)]))
[(1, 11), (2, 22), (3, 33)]
The caller should ensure that the dimensions of the input are compatible.
If the input is empty, no output will be produced.
)<01> _zip_strict<63>r<>s&r`rTrT<00>s<00><00> <17><02> <1B>rbc<04>T<00>\V4\Y4# \dR#i;i)z.Scalars are bytes, strings, and non-iterables.T)rsrr<00>
isinstance)r<00>
stringlikes&&r`<00>
_is_scalarr,s/<00><00><14> <0C>U<EFBFBD> <0B> <16>e<EFBFBD> (<28>(<28><> <15><14><13><14>s <00> <00> '<03>'c<04><><00>\V4p\V4p\T3T4p\ T4'dT#\P
!T4pKE \dTu#i;i)z.Depth-first iterator over scalars in a tensor.)rsrv<00> StopIterationr r,r<>)<03>tensorrwrs& r`<00>_flatten_tensorr0 sb<00><00><13>F<EFBFBD>|<7C>H<EFBFBD>
<0E> <1C><18><18>N<EFBFBD>E<EFBFBD><19>%<25><18>8<EFBFBD>,<2C><08> <15>e<EFBFBD> <1C> <1C><1B>O<EFBFBD><18>&<26>&<26>x<EFBFBD>0<><08><> <1D> <1C><1B>O<EFBFBD> <1C>s<00> A<00> A!<03> A!c<04><><00>\V\4'd!\\P!V4V4#Vvr#\ V4p\ \\V4V4p\WR4#)a<>Change the shape of a *matrix*.
If *shape* is an integer, the matrix must be two dimensional
and the shape is interpreted as the desired number of columns:
>>> matrix = [(0, 1), (2, 3), (4, 5)]
>>> cols = 3
>>> list(reshape(matrix, cols))
[(0, 1, 2), (3, 4, 5)]
If *shape* is a tuple (or other iterable), the input matrix can have
any number of dimensions. It will first be flattened and then rebuilt
to the desired shape which can also be multidimensional:
>>> matrix = [(0, 1), (2, 3), (4, 5)] # Start with a 3 x 2 matrix
>>> list(reshape(matrix, (2, 3))) # Make a 2 x 3 matrix
[(0, 1, 2), (3, 4, 5)]
>>> list(reshape(matrix, (6,))) # Make a vector of length six
[0, 1, 2, 3, 4, 5]
>>> list(reshape(matrix, (2, 1, 3, 1))) # Make 2 x 1 x 3 x 1 tensor
[(((0,), (1,), (2,)),), (((3,), (4,), (5,)),)]
Each dimension is assumed to be uniform, either all arrays or all scalars.
Flattening stops when the first value in a dimension is a scalar.
Scalars are bytes, strings, and non-iterables.
The reshape iterator stops when the requested shape is complete
or when the input is exhausted, whichever comes first.
) r*<00>intr(r r<>r0r <00>reversedr)<06>matrix<69>shape<70> first_dim<69>dims<6D> scalar_stream<61>reshapeds&& r`rDrDsY<00><00>B<12>%<25><13><1D><1D><16>u<EFBFBD>*<2A>*<2A>6<EFBFBD>2<>E<EFBFBD>:<3A>:<3A><1C><14>I<EFBFBD>#<23>F<EFBFBD>+<2B>M<EFBFBD><15>g<EFBFBD>x<EFBFBD><04>~<7E>}<7D>=<3D>H<EFBFBD> <11>(<28> &<26>&rbc <04><><00>\V^,4p\\\\ V\ V444V4#)a Multiply two matrices.
>>> list(matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]))
[(49, 80), (41, 60)]
The caller should ensure that the dimensions of the input matrices are
compatible with each other.
Supports all numeric types: int, float, complex, Decimal, Fraction.
)rpr(rrrrT)<03>m1<6D>m2rgs&& r`r5r5@s0<00><00> <0C>B<EFBFBD>q<EFBFBD>E<EFBFBD>
<EFBFBD>A<EFBFBD> <12>7<EFBFBD>8<EFBFBD>W<EFBFBD>R<EFBFBD><19>2<EFBFBD><1D>%?<3F>@<40>!<21> D<>Drbc<00><00>\^V4Fop^;r#^pV^8XdWW",V,V,pW3,V,V,pW3,V,V,p\W#,
V4pK]W@8wgKmVu# \R4h)r<>zprime or under 5)r<>rr<>)rgr<>r^r_<00>ds& r`<00>_factor_pollardr?Osy<00><00><13>1<EFBFBD>a<EFBFBD>[<5B><01><11> <09><01> <0A><01><0F>1<EFBFBD>f<EFBFBD><12><15><11><19>a<EFBFBD><0F>A<EFBFBD><12><15><11><19>a<EFBFBD><0F>A<EFBFBD><12><15><11><19>a<EFBFBD><0F>A<EFBFBD><13>A<EFBFBD>E<EFBFBD>1<EFBFBD> <0A>A<EFBFBD> <0C>6<EFBFBD><14>H<EFBFBD><19> <15>'<27>
(<28>(rbc#<04>V"<00>V^8dR#\FpW,'dKVx<00>W,pK .pV^8<>dV.M.pVFIpVR8g\V4'dVPV4K.\V4pW4W,3, pKK \ V4Rjx<01>L
R#L5i)z<>Yield the prime factors of n.
>>> list(factor(360))
[2, 2, 2, 3, 3, 5]
Finds small factors with trial division. Larger factors are
either verified as prime with ``is_prime`` or split into
smaller factors with Pollard's rho algorithm.
Ni<EFBFBD><EFBFBD>)<05>_primes_below_211r1r<>r?r<>)rg<00>prime<6D>primes<65>todo<64>facts& r`r.r.bs<><00><00><00> <09>1<EFBFBD>u<EFBFBD><0E>#<23><05><13>)<29>)<29><17>K<EFBFBD> <0A>K<EFBFBD>A<EFBFBD>#<23> <10>F<EFBFBD><13>a<EFBFBD>%<25>A<EFBFBD>3<EFBFBD>R<EFBFBD>D<EFBFBD> <11><01> <0C>v<EFBFBD>:<3A><18>!<21><1B><1B> <12>M<EFBFBD>M<EFBFBD>!<21> <1C>"<22>1<EFBFBD>%<25>D<EFBFBD> <10>1<EFBFBD>9<EFBFBD>%<25> %<25>D<EFBFBD> <12> <16>f<EFBFBD>~<7E><1D><1D>s<00>B)<01>A;B)<01> B'<04>!B)c <04><><00>\V4pV^8Xd\V4!^4#\\\ V4\ \ V444p\W4#)a<>Evaluate a polynomial at a specific value.
Computes with better numeric stability than Horner's method.
Evaluate ``x^3 - 4 * x^2 - 17 * x + 60`` at ``x = 2.5``:
>>> coefficients = [1, -4, -17, 60]
>>> x = 2.5
>>> polynomial_eval(coefficients, x)
8.125
Note that polynomial coefficients are specified in descending power order.
Supports all numeric types: int, float, complex, Decimal, Fraction.
)rp<00>typerj<00>powrr3r<>r)<04> coefficientsr^rg<00>powerss&& r`r>r><00>sI<00><00> <0C>L<EFBFBD><19>A<EFBFBD><08>A<EFBFBD>v<EFBFBD><13>A<EFBFBD>w<EFBFBD>q<EFBFBD>z<EFBFBD><19> <10><13>f<EFBFBD>Q<EFBFBD>i<EFBFBD><18>%<25><01>(<28>!3<> 4<>F<EFBFBD> <13>L<EFBFBD> )<29>)rbc<04>&<00>\\V4!#)z<>Return the sum of the squares of the input values.
>>> sum_of_squares([10, 20, 30])
1400
Supports all numeric types: int, float, complex, Decimal, Fraction.
)rrr(s&r`rOrO<00>s<00><00> <14>S<EFBFBD><12>W<EFBFBD> <1D>rbc<04>t<00>\V4p\\^V44p\\ \
W44#)u<>Compute the first derivative of a polynomial.
Evaluate the derivative of ``x³ - 4 x² - 17 x + 60``:
>>> coefficients = [1, -4, -17, 60]
>>> derivative_coefficients = polynomial_derivative(coefficients)
>>> derivative_coefficients
[3, -8, -17]
Note that polynomial coefficients are specified in descending power order.
Supports all numeric types: int, float, complex, Decimal, Fraction.
)rpr3r<>rfrjr)rIrgrJs& r`r@r@<00>s0<00><00> <0C>L<EFBFBD><19>A<EFBFBD> <15>e<EFBFBD>A<EFBFBD>q<EFBFBD>k<EFBFBD> "<22>F<EFBFBD> <0F><03>C<EFBFBD><1C>.<2E> /<2F>/rbc<04>Z<00>\\V44FpWV,,pK V#)u<>Return the count of natural numbers up to *n* that are coprime with *n*.
Euler's totient function φ(n) gives the number of totatives.
Totative are integers k in the range 1 ≤ k ≤ n such that gcd(n, k) = 1.
>>> n = 9
>>> totient(n)
6
>>> totatives = [x for x in range(1, n) if gcd(n, x) == 1]
>>> totatives
[1, 2, 4, 5, 7, 8]
>>> len(totatives)
6
Reference: https://en.wikipedia.org/wiki/Euler%27s_totient_function
)r<>r.)rgrBs& r`rSrS<00>s&<00><00>&<15>V<EFBFBD>A<EFBFBD>Y<EFBFBD><1E><05> <09>%<25>Z<EFBFBD><0F><01> <20> <0C>Hrbc<04><><00>V^,
V, P4^,
pW, p^V,V,V8XdV^,'dV^8<>gQhW3#)z#Return s, d such that 2**s * d == n)<01>
bit_length)rgr<>r>s& r`<00> _shift_to_oddrP<00>sQ<00><00> <0C>a<EFBFBD>%<25>1<EFBFBD><1B> <20> <20>"<22>Q<EFBFBD>&<26>A<EFBFBD> <09><06>A<EFBFBD> <0A><11>F<EFBFBD>a<EFBFBD><<3C>1<EFBFBD> <1C><11>Q<EFBFBD><15><15>1<EFBFBD><01>6<EFBFBD>1<> 1<> <0C>4<EFBFBD>Krbc<00>6<00>V^8<>d"V^,'d^Tu;8:d V8gQhQh\V^,
4wr#\WV4pV^8XgW@^,
8XdR#\V^,
4F"pWD,V,pW@^,
8XgK!R# R#)<03>TF)rPrHr<>)rg<00>baser<65>r>r^<00>_s&& r`<00>_strong_probable_primerU<00>s<><00><00> <0A><01>E<EFBFBD><01>A<EFBFBD><05><05>A<EFBFBD><14>M<EFBFBD><01>M<EFBFBD>2<> 2<>M<EFBFBD>2<> 2<> <18><11>Q<EFBFBD><15> <1F>D<EFBFBD>A<EFBFBD> <0B>D<EFBFBD>Q<EFBFBD><0F>A<EFBFBD><08>A<EFBFBD>v<EFBFBD><11>!<21>e<EFBFBD><1A><13> <12>1<EFBFBD>q<EFBFBD>5<EFBFBD>\<5C><01> <0A>E<EFBFBD>A<EFBFBD>I<EFBFBD><01> <0C>A<EFBFBD><05>:<3A><17><1A>
rbc<04><>a<00>S^8dSR9#S^,'dLS^,'d=S^,'d.S^,'dS^ ,'dS^ ,'gR#\FwrSV8gKM V3Rl\^@44p\;QJdV3RlV4F 'dK R# R#!V3RlV44#)amReturn ``True`` if *n* is prime and ``False`` otherwise.
Basic examples:
>>> is_prime(37)
True
>>> is_prime(3 * 13)
False
>>> is_prime(18_446_744_073_709_551_557)
True
Find the next prime over one billion:
>>> next(filter(is_prime, count(10**9)))
1000000007
Generate random primes up to 200 bits and up to 60 decimal digits:
>>> from random import seed, randrange, getrandbits
>>> seed(18675309)
>>> next(filter(is_prime, map(getrandbits, repeat(200))))
893303929355758292373272075469392561129886005037663238028407
>>> next(filter(is_prime, map(randrange, repeat(10**60))))
269638077304026462407872868003560484232362454342414618963649
This function is exact for values of *n* below 10**24. For larger inputs,
the probabilistic Miller-Rabin primality test has a less than 1 in 2**128
chance of a false positive.
Fc3<00>J<"<00>TFp\^S^,
4x<00>K R#5i)rRN)<01>_private_randranger<65>s& <20>r`r<><00>is_prime.<locals>.<genexpr>*s <00><><00><00>A<>y<EFBFBD>!<21>#<23>A<EFBFBD>q<EFBFBD>1<EFBFBD>u<EFBFBD>-<2D>-<2D>y<EFBFBD>s<00> #c3<00><<"<00>TFp\SV4x<00>K R#5ir])rU)r<>rSrgs& <20>r`r<>rY,s<00><><00><00>A<>5<EFBFBD>4<EFBFBD>%<25>a<EFBFBD><14>.<2E>.<2E>5<EFBFBD>r<EFBFBD>T>rR<00><00><00><00> <00> )<03>_perfect_testsr<73><00>all)rg<00>limit<69>basessf r`r1r1<00>s<><00><><00>B <09>2<EFBFBD>v<EFBFBD><10>(<28>(<28>(<28> <0A><01>E<EFBFBD>E<EFBFBD>a<EFBFBD>!<21>e<EFBFBD>e<EFBFBD><01>A<EFBFBD><05><05>!<21>a<EFBFBD>%<25>%<25>A<EFBFBD><02>F<EFBFBD>F<EFBFBD>q<EFBFBD>2<EFBFBD>v<EFBFBD>v<EFBFBD><14>&<26> <0C><05> <0C>u<EFBFBD>9<EFBFBD> <11>'<27>B<01>u<EFBFBD>R<EFBFBD>y<EFBFBD>A<><05> <0E>3<EFBFBD>A<>5<EFBFBD>A<>3<EFBFBD>3<EFBFBD>A<>3<EFBFBD>A<>3<EFBFBD>A<>5<EFBFBD>A<> A<>Arbc<04><00>\RV4#)z<>Returns an iterable with *n* elements for efficient looping.
Like ``range(n)`` but doesn't create integers.
>>> i = 0
>>> for _ in loops(5):
... i += 1
>>> i
5
N)r)rgs&r`r4r4/s<00><00> <12>$<24><01>?<3F>rbc<04>H<00>\\\\V4V44#)ueNumber of distinct arrangements of a multiset.
The expression ``multinomial(3, 4, 2)`` has several equivalent
interpretations:
* In the expansion of ``(a + b + c)⁹``, the coefficient of the
``a³b⁴c²`` term is 1260.
* There are 1260 distinct ways to arrange 9 balls consisting of 3 reds, 4
greens, and 2 blues.
* There are 1260 unique ways to place 9 distinct objects into three bins
with sizes 3, 4, and 2.
The :func:`multinomial` function computes the length of
:func:`distinct_permutations`. For example, there are 83,160 distinct
anagrams of the word "abracadabra":
>>> from more_itertools import distinct_permutations, ilen
>>> ilen(distinct_permutations('abracadabra'))
83160
This can be computed directly from the letter counts, 5a 2b 2r 1c 1d:
>>> from collections import Counter
>>> list(Counter('abracadabra').values())
[5, 2, 2, 1, 1]
>>> multinomial(5, 2, 2, 1, 1)
83160
A binomial coefficient is a special case of multinomial where there are
only two categories. For example, the number of ways to arrange 12 balls
with 5 reds and 7 blues is ``multinomial(5, 7)`` or ``math.comb(12, 5)``.
Likewise, factorial is a special case of multinomial where
the multiplicities are all just 1 so that
``multinomial(1, 1, 1, 1, 1, 1, 1) == math.factorial(7)``.
Reference: https://en.wikipedia.org/wiki/Multinomial_theorem
)rrjrr )<01>countss*r`r6r6=s<00><00>T <10><03>D<EFBFBD>*<2A>V<EFBFBD>,<2C>f<EFBFBD>5<> 6<>6rbc
#<04>F"<00>VPp.p.p\\4;_uu_4\V\ W1!444V^,x<00>\ V\ W!!444V^,V^,,^, x<00>Kb +'giR#;i5i)z.Non-windowed running_median() for Python 3.14+N)<07>__next__rr.rcr r
rd<00>rw<00>read<61>lo<6C>his& r`<00>#_running_median_minheap_and_maxheaprmjs{<00><00><00> <14> <1C> <1C>D<EFBFBD> <0B>B<EFBFBD> <0B>B<EFBFBD> <11>-<2D> <20> <20><12> <18><12>[<5B><12>T<EFBFBD>V<EFBFBD>4<> 5<><14>Q<EFBFBD>%<25>K<EFBFBD> <14>R<EFBFBD><1F><12>T<EFBFBD>V<EFBFBD>4<> 5<><15>a<EFBFBD>5<EFBFBD>2<EFBFBD>a<EFBFBD>5<EFBFBD>=<3D>A<EFBFBD>%<25> %<25>
!<21> <20> <20>s<00>(B!<01>A#B <05> B <09> B!c
#<04>N"<00>VPp.p.p\\4;_uu_4\V\ W1!44)4V^,)x<00>\V\ W!!4)4)4V^,V^,,
^, x<00>Kf +'giR#;i5i)zDBackport of non-windowed running_median() for Python 3.13 and prior.N)rhrr.r
r ris& r`<00>_running_median_minheap_onlyrozs<><00><00><00> <14> <1C> <1C>D<EFBFBD> <0B>B<EFBFBD> <0B>B<EFBFBD> <11>-<2D> <20> <20><12> <14>R<EFBFBD>+<2B>b<EFBFBD>$<24>&<26>1<>1<> 2<><15>a<EFBFBD>5<EFBFBD>&<26>L<EFBFBD> <14>R<EFBFBD>+<2B>b<EFBFBD>4<EFBFBD>6<EFBFBD>'<27>2<>2<> 3<><15>a<EFBFBD>5<EFBFBD>2<EFBFBD>a<EFBFBD>5<EFBFBD>=<3D>A<EFBFBD>%<25> %<25>
!<21> <20> <20>s<00>(B%<01>A'B<05> B" <09> B%c#<04>f"<00>\4p.pVF<>pVPV4\W44\V4V8<>d\ W2P 44pW5\V4pV^,pV^,'d W7,M"W7^,
,W7,,^, x<00>K<> R#5i)z+Yield median of values in a sliding window.N)rr<>rrpr<00>popleft)rwror<00>orderedr^r<>rg<00>ms&& r`<00>_running_median_windowedrt<00>s<><00><00><00><13>W<EFBFBD>F<EFBFBD><10>G<EFBFBD> <15><01><0E> <0A> <0A>a<EFBFBD><18><0E>w<EFBFBD><1A> <0E>w<EFBFBD><<3C>&<26> <20><1B>G<EFBFBD>^<5E>^<5E>%5<>6<>A<EFBFBD><17>
<EFBFBD> <0F><07>L<EFBFBD><01> <0A><11>F<EFBFBD><01><1D><01>E<EFBFBD>E<EFBFBD>g<EFBFBD>j<EFBFBD><07>A<EFBFBD><05><0E><17><1A>(C<>q<EFBFBD>'H<>H<><16>s<00>B/B1roc<04><><00>\V4pVe)\V4pV^8:d \R4h\W!4#\'g \ V4#\ V4#)aCumulative median of values seen so far or values in a sliding window.
Set *maxlen* to a positive integer to specify the maximum size
of the sliding window. The default of *None* is equivalent to
an unbounded window.
For example:
>>> list(running_median([5.0, 9.0, 4.0, 12.0, 8.0, 9.0]))
[5.0, 7.0, 5.0, 7.0, 8.0, 8.5]
>>> list(running_median([5.0, 9.0, 4.0, 12.0, 8.0, 9.0], maxlen=3))
[5.0, 7.0, 5.0, 9.0, 8.0, 9.0]
Supports numeric types such as int, float, Decimal, and Fraction,
but not complex numbers which are unorderable.
On version Python 3.13 and prior, max-heaps are simulated with
negative values. The negation causes Decimal inputs to apply context
rounding, making the results slightly different than that obtained
by statistics.median().
zWindow size should be positive)rsr"r<>rt<00>_max_heap_availablerorm)rhrorws&$ r`rKrK<00>sV<00><00>.<14>H<EFBFBD>~<7E>H<EFBFBD> <0A><19><16>v<EFBFBD><1D><06> <11>Q<EFBFBD>;<3B><1C>=<3D>><3E> ><3E>'<27><08>9<>9<> <1E> <1E>+<2B>H<EFBFBD>5<>5<> .<2E>x<EFBFBD> 8<>8rb)r<>r])r<>N)NF)NN)r<>N))i<>)rR)i<69><7F>)<02><00>I)l<00>tT7)rRr]<00>=)l<00>ay)rRr_<00>iS_)l;n><3E>)rRr[r\r]r^)l<00>p<0E> )rRr[r\r]r^r_)l)rRiEi<>$ini<><69>i=<3D>i<><69>k)l<00>%!H<>n <0A>fW) rRr[r\r]r^r_<00><00>rz<00>rw<00>%<00>))<29><>__doc__<5F>random<6F>bisectrr<00> collectionsr<00>
contextlibr<00> functoolsrrr <00>heapqr
r <00> itertoolsr r rrrrrrrrrrrr<00>mathrrrr<00>operatorrrr r!r"r#r$r%<00>sysr&<00>__all__<5F>objectr<74>r<>r'rrr[r<00> ImportErrorrcrdrvrRrPrQr*r8r'r<>rCr;r:r7r,r/rIr<>r<r<>r<>r<>r<>r<>r0rJr=rArWrXrVr2r-rHrGrFrEr9rBr+r)rUr rrMrNr?r3rLr#r(r%rT<00>strrr,r0rDr5r?r<>rAr.r>rOr@rSr`rPrU<00>RandomrXr1r4r6rmrortrKr<>rbr`<00><module>r<>s<><00><01><04><0E>&<26><1D><1F>0<>0<>'<27><02><02><02><02> (<28>'<27>:<3A>:<3A>,<2C>,<2C><1A>3 <02><0F>3 <02> <0A>3 <02><17>3 <02><0E> 3 <02>
<0F> 3 <02> <11> 3 <02><11>3 <02> <0A>3 <02><0E>3 <02><0E>3 <02><0F>3 <02><12>3 <02><11>3 <02> <0C>3 <02> <0A>3 <02> <12>!3 <02>"<0E>#3 <02>$
<EFBFBD>%3 <02>&<16>'3 <02>(<0E>)3 <02>*<0F>+3 <02>,<0F>-3 <02>.<10>/3 <02>0<16>13 <02>2<1C>33 <02>4<1C>53 <02>6<0F>73 <02>8<0E>93 <02>:<0F>;3 <02><<0E>=3 <02>>*<2A>?3 <02>@<19>A3 <02>B<19>C3 <02>D<15>E3 <02>F<11>G3 <02>H<11>I3 <02>J<15>K3 <02>L <0C>M3 <02>N<15>O3 <02>P<10>Q3 <02>R<15>S3 <02>T<0F>U3 <02>V <0B>W3 <02>X <0B>Y3 <02>Z<0E>[3 <02>\<10>]3 <02>^<11>_3 <02>` <0A>a3 <02>b<16>c3 <02>d<16>e3 <02><07>j <11>(<28><07>,<2C><07>t<EFBFBD><14><1A>#<23>d<EFBFBD>+<2B>K<EFBFBD>-<2D>(<28> <1F>3<><1F><17> %<25> '<27>$ 8<> %+<2B>P
4<><10>4!<21>$<24> )<29> <13><07>;<3B>
%<25> ,<2C>.<2E>6 <15> )<29>8<>
,<2C>!<21>(<28>(<28>H<EFBFBD><14><1E>J<EFBFBD><1E><14> /<2F> %=<3D>P(<28>$;<3B>8N<01>**<1E>Z A<01>/<2F>(<19>:1<>(1<><11>1<>("<22>$ +<2B> +<2B>"'<19>T $<24>('<27>V<18>& <1B> <1B><1C>?<3F>2 -<2D><10>,&;<3B>R*<2A>*<14>E<EFBFBD><14>(<0E><19><1A>6<>=<3D>u<EFBFBD>=<3D><1F>&<26>&<26>G<EFBFBD>O<EFBFBD><16>G<EFBFBD> <1C>#&<26>u<EFBFBD><1C>)<29> 1<>&'<27>R E<01> )<29> <1A>%<25><03>*<2A>%<25><11><1E>B*<2A>.<1E>0<>& <0A>2 <02><0E> <0B><10> <0B><10><11>&<1C>]<5D>]<5D>_<EFBFBD>.<2E>.<2E><12>-B<01>` <1B>*7<>Z &<26> &<26> I<01>&"9<>t<EFBFBD>"9<><39>w)<11><16><15>K<EFBFBD><16><><13>-<2D>,<2C>H<EFBFBD>-<2D><><13> <20><1F><17> <20><>`<13><19><18>H<EFBFBD><19>sH<00>% I3<00>9J<00>J<00>J$<00>3 J<03>?J<03>
J<03>J<03> J!<03> J!<03>$ J1<03>0J1