Ë s/uèš ëãóô—dZddlZddlZddlmZddlmZddlmZm Z ddl m Z m Z m Z mZmZmZmZmZmZmZmZmZddlmZmZmZddlmZgd ¢Ze«Z e d ¬ «ee d ¬ «Z!e#ed d „«Z$d„Z%dFd„Z&d„Z'dGd„Z(dGd„Z)d„Z*e+fd„Z,d„Z-e-Z.d„Z/d„Z0d„Z1dGd„Z2d„Z3 ddl m4Z5d„Z4e3je4_Gd„de7«Z8d„Z9d „Z:dHd!„Z;d"„ZdGd%„Z?dGd&„Z@dGd'„ZAdId(„ZBd)d*œd+„ZCdGd,„ZDd-„ZEd.„ZFd/„ZGd0„ZHd1„ZId2„ZJd3„ZKd4„ZLd5„ZMd6„ZNdJd7„ZOd8„ZPd9d œd:„ZQed;k\r dd„ZTd?„ZUd@„ZVdA„ZWdB„ZXdC„ZYdD„ZZdE„Z[y#e"$re Z!YŒÿwxYw#e6$re3Z4YŒ·wxYw)KaImported 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)Údeque)ÚSized)ÚpartialÚreduce) ÚchainÚ combinationsÚcompressÚcountÚcycleÚgroupbyÚisliceÚproductÚrepeatÚstarmapÚteeÚ zip_longest)Ú randrangeÚsampleÚchoice)Ú hexversion)-Ú all_equalÚbatchedÚbefore_and_afterÚconsumeÚconvolveÚ dotproductÚ first_trueÚfactorÚflattenÚgrouperÚ iter_exceptÚ iter_indexÚmatmulÚncyclesÚnthÚnth_combinationÚpadnoneÚpad_noneÚpairwiseÚ partitionÚpolynomial_evalÚpolynomial_from_rootsÚpolynomial_derivativeÚpowersetÚprependÚquantifyÚreshapeÚ#random_combination_with_replacementÚrandom_combinationÚrandom_permutationÚrandom_productÚ repeatfuncÚ roundrobinÚsieveÚsliding_windowÚ subslicesÚsum_of_squaresÚtabulateÚtailÚtakeÚtotientÚ transposeÚ triplewiseÚunique_everseenÚunique_justseenT©ÚstrictÚsumprodcó—t||«S©N)r)ÚxÚys ú;/usr/lib/python3.12/site-packages/more_itertools/recipes.pyÚrL]s €´¸A¸qÔ1Aócó,—tt||««S)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] )Úlistr ©ÚnÚiterables rKr>r>`s€ô ”�x Ó#Ó $Ð$rMcó,—t|t|««S)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] )Úmapr )ÚfunctionÚstarts rKr<r<ps€ô ˆxœ˜u›Ó &Ð&rMc #óÈK—t|t«r,t|tdt |«|z «d«Ed{–—†yt t ||¬««Ed{–—†y7Œ$7Œ­w)zƒReturn an iterator over the last *n* items of *iterable*. >>> t = tail(3, 'ABCDEFG') >>> list(t) ['E', 'F', 'G'] rN©Úmaxlen)Ú isinstancerr ÚmaxÚlenÚiterrrPs rKr=r=‚sSèø€ô�(œEÔ"ܘ(¤C¨¬3¨x«=¸1Ñ+<Ó$=¸tÓD×DÑD䜘h¨qÔ1Ó2×2Ñ2ð Eøà2ús!‚7A"¹AºA"ÁA ÁA"Á A"cóR—|€t|d¬«ytt|||«d«y)aXAdvance *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 "", line 1, in 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 "", line 1, in StopIteration NrrX)rÚnextr )ÚiteratorrQs rKrr”s)€ð@ €yä ˆh˜qÖ!ô ŒV�H˜a Ó # TÕ*rMcó0—tt||d«|«S)z…Returns the nth item or a default value. >>> l = range(10) >>> nth(l, 3) 3 >>> nth(l, 20, "zebra") 'zebra' N)r_r )rRrQÚdefaults rKr%r%¼s€ô ”�x  DÓ)¨7Ó 3Ð3rMcóN—t|«}t|d«xr t|d« S)z  Returns ``True`` if all the elements are equal to each other. >>> all_equal('aaaa') True >>> all_equal('aaab') False TF)r r_)rRÚgs rKrrÉs(€ô �Ó€AÜ ��4‹=Ò /¤ a¨£Ð/Ð/rMcó,—tt||««S)zcReturn the how many times the predicate is true. >>> quantify([True, False, True]) 2 )ÚsumrT)rRÚpreds rKr0r0×s€ô Œs�4˜Ó"Ó #Ð#rMcó,—t|td««S)aReturns 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)rr©rRs rKr(r(ás€ô �œ6 $›<Ó (Ð(rMcóR—tjtt|«|««S)zvReturns the sequence elements *n* times >>> list(ncycles(["a", "b"], 3)) ['a', 'b', 'a', 'b', 'a', 'b'] )rÚ from_iterablerÚtuple)rRrQs rKr$r$òs €ô × Ñ œv¤e¨H£o°qÓ9Ó :Ð:rMcóJ—tttj||««S)zcReturns the dot product of the two iterables. >>> dotproduct([10, 10], [20, 20]) 400 )rfrTÚoperatorÚmul)Úvec1Úvec2s rKrrüs€ô Œs”8—<‘<  tÓ,Ó -Ð-rMcó,—tj|«S)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. )rrk)Ú listOfListss rKrrs€ô × Ñ ˜{Ó +Ð+rMcó\—|€t|t|««St|t||««S)aGCall *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)ÚfuncÚtimesÚargss rKr6r6s.€ð, €}Ü�tœV D›\Ó*Ð*Ü �4œ  eÓ,Ó -Ð-rMcóN—t|«\}}t|d«t||«S)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)rr_Úzip)rRÚaÚbs rKÚ _pairwiser|-s&€ô ˆx‹=�D€A€q܈ˆD„MÜ ˆq�!‹9ÐrM©r)có—t|«SrH)Úitertools_pairwiseris rKr)r)As €Ü! (Ó+Ð+rMcó ‡—eZdZdˆfd„ ZˆxZS)ÚUnequalIterablesErrorcóP•—d}|�|dj|Žz }t‰|� |«y)Nz Iterables have different lengthsz/: index 0 has length {}; index {} has length {})ÚformatÚsuperÚ__init__)ÚselfÚdetailsÚmsgÚ __class__s €rKr…zUnequalIterablesError.__init__Hs;ø€Ø0ˆØ Ð Ø ÐMÐE×MÑMØðñ ˆCô ‰Ñ˜ÕrMrH)Ú__name__Ú __module__Ú __qualname__r…Ú __classcell__)r‰s@rKr�r�Gs ø„÷ñrMr�c#ónK—t|dtiŽD]}|D]}|tusŒ t«‚|–—Œ!y­w)NÚ fillvalue)rÚ_markerr�)Ú iterablesÚcomboÚvals rKÚ_zip_equal_generatorr”Rs;èø€Ü˜iÐ;´7Ô;ˆÛˆCØ”gŠ~Ü+Ó-Ð-ðð‹ ñ <ùs‚ 5£5cóЗ t|d«}t|ddd«D]$\}}t|«}||k7sŒt|||f¬«‚t|ŽS#t$rt |«cYSwxYw)Nré)r‡)r\Ú enumerater�ryÚ TypeErrorr”)r‘Ú first_sizeÚiÚitÚsizes rKÚ _zip_equalr�Zs{€ð /ܘ 1™Ó&ˆ ܘy¨¨˜}¨aÖ0‰EˆAˆrÜ�r“7ˆDØ�zÓ!Ü+°ZÀÀDÐ4IÔJÐJð1ô �IˆÐøô ò/Ü# IÓ.Ò.ð/ús‚3A¶AÁA%Á$A%cóŠ—t|«g|z}|dk(r t|d|iŽS|dk(rt|ŽS|dk(rt|ŽSt d«‚)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. >>> it = grouper('ABCDEFG', 3, incomplete='strict') >>> list(it) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... UnequalIterablesError Úfillr�rEÚignorez Expected fill, strict, or ignore)r]rr�ryÚ ValueError)rRrQÚ incompleter�rws rKr r js^€ô: �‹NÐ ˜aÑ €DØ�VÒܘDÐ6¨IÑ6Ð6Ø�XÒܘ4Ð Ð Ø�XÒÜ�DˆzÐäÐ;Ó<Ð>> 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). c3óFK—|]}t|«j–—Œy­wrH)r]Ú__next__)Ú.0r›s rKÚ zroundrobin..Ÿsèø€Ð8©i¨”$�r“(×#Õ#©iùs‚!r–N)r\r Ú StopIterationr )r‘ÚpendingÚnextsr_s rKr7r7’sièø€ô�)‹n€GÜ Ñ8©iÓ8Ó 8€EÙ ð 2Û�Ù“f“ ñô øôò 2Ø �q‰LˆGÜœ& ¨Ó0Ó1ŠEð 2üs*‚ A"£9³A"·A"¹#AÁA"ÁAÁA"có—|€t}t|d«\}}}tt||««\}}t|ttj |««t||«fS)a¯ 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, ' ']) é)ÚboolrrTr rnÚnot_)rgrRÚt1Út2ÚpÚp1Úp2s rKr*r*©sX€ð( €|܈ä�H˜aÓ �I€BˆˆAÜ ”�T˜1“Ó �F€BˆÜ �RœœXŸ]™]¨BÓ/Ó 0´(¸2¸rÓ2BÐ CÐCrMc󀇗t|«Štjˆfd„tt ‰«dz«D««S)aœYields 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. Use :func:`unique_everseen` on the input to avoid generating duplicates: >>> seq = [1, 1, 0] >>> list(powerset(seq)) [(), (1,), (1,), (0,), (1, 1), (1, 0), (1, 0), (1, 1, 0)] >>> from more_itertools import unique_everseen >>> list(powerset(unique_everseen(seq))) [(), (1,), (0,), (1, 0)] c3ó6•K—|]}t‰|«–—Œy­wrH)r)r¦ÚrÚss €rKr§zpowerset..Ùsøèø€ÐMÑ;L°aœ|¨A¨q×1Ñ;Lùsƒr–)rOrrkÚranger\)rRr·s @rKr.r.Ås2ø€ô& ˆX‹€AÜ × Ñ ÓM¼5ÄÀQÃÈ!ÁÔ;LÓMÓ MÐMrMc#óâK—t«}|j}g}|j}|du}|D]}|r||«n|} ||vr ||«|–—Œ!y#t$r||vr ||«|–—YŒ>wxYw­w)a– 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)ÚsetÚaddÚappendr˜) rRÚkeyÚseensetÚ seenset_addÚseenlistÚ seenlist_addÚuse_keyÚelementÚks rKrBrBÜs…èø€ô6‹e€GØ—+‘+€KØ€HØ—?‘?€Lؘˆo€GãˆÙ#‰C�ŒL¨ˆð ؘÑÙ˜A”Ø’ øñ øô ò Ø˜Ñ Ù˜Q”Ø’ ùð üs(‚:A/½AÁ A/ÁA,Á)A/Á+A,Á,A/c óÄ—|€(ttjd«t|««Sttttjd«t||«««S)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'] rr–)rTrnÚ itemgetterr r_)rRr½s rKrCrC sL€ð €{Ü”8×&Ñ& qÓ)¬7°8Ó+<Ó=Ð=ä Œt”Sœ×,Ñ,¨QÓ/´¸À3Ó1GÓHÓ IÐIrMc#óNK— |� |«–— |«–—Œ #|$rYywxYw­w)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))) [] N©)ruÚ exceptionÚfirsts rKr!r!s8èø€ð, Ø Ð Ù“'ŠMØÙ“&ŠLðøà ò Ù ð üs‚%„š"Ÿ%¡"¢%có.—tt||«|«S)a� 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' )r_Úfilter)rRrbrgs rKrr7s€ô" ”�t˜XÓ&¨Ó 0Ð0rMr–)rcóh—|D�cgc] }t|«‘Œc}|z}td„|D««Scc}w)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, **kwarg)``. c3ó2K—|]}t|«–—Œy­wrH)r)r¦Úpools rKr§z!random_product..\sèø€Ð0©% $”˜—©%ùs‚)rl)rrwrÏÚpoolss rKr5r5Ks7€ñ &*Ó *¡T˜TŒU�4�[ TÑ *¨VÑ 3€EÜ Ñ0©%Ó0Ó 0Ð0ùò +s…/có`—t|«}|€ t|«n|}tt||««S)abReturn 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)``. )rlr\r)rRr¶rÏs rKr4r4_s-€ô �‹?€DØ�YŒˆDŒ  A€AÜ ”˜˜a“Ó !Ð!rMc󔇗t|«Št‰«}ttt |«|««}tˆfd„|D««S)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ó(•K—|] }‰|–—Œ y­wrHrÈ©r¦ršrÏs €rKr§z%random_combination..~óøèø€Ð*¡'˜Q��a•¡'ùóƒ)rlr\Úsortedrr¸)rRr¶rQÚindicesrÏs @rKr3r3qs=ø€ô �‹?€DÜ ˆD‹ €AÜ”VœE !›H aÓ(Ó)€GÜ Ó*¡'Ó*Ó *Ð*rMc󔇇—t|«Št‰«Štˆfd„t|«D««}tˆfd„|D««S)aSReturn 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ó4•K—|]}t‰«–—Œy­wrH)r)r¦ršrQs €rKr§z6random_combination_with_replacement..Žsøèø€Ð4©8 a”Y˜q—\©8ùsƒc3ó(•K—|] }‰|–—Œ y­wrHrÈrÔs €rKr§z6random_combination_with_replacement..�rÕrÖ)rlr\r×r¸)rRr¶rØrQrÏs @@rKr2r2�s<ù€ô �‹?€DÜ ˆD‹ €AÜÓ4¬5°¬8Ó4Ó4€GÜ Ó*¡'Ó*Ó *Ð*rMcóž—t|«}t|«}|dks||kDrt‚d}t|||z «}t d|dz«D]}|||z |zz|z}Œ|dkr||z }|dks||k\rt ‚g}|rL||z|z|dz |dz }}}||k\r||z}|||z z|z|dz }}||k\rŒ|j |d|z «|rŒLt|«S)aEquivalent 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. rr–éÿÿÿÿ)rlr\r¡Úminr¸Ú IndexErrorr¼) rRr¶ÚindexrÏrQÚcrÄršÚresults rKr&r&’s €ô �‹?€DÜ ˆD‹ €AØ ˆAŠ�1�q’5ÜÐà €AÜ ˆAˆq�1‰u‹ €AÜ �1�a˜!‘eŽ_ˆØ ��Q‘˜‘‰O˜qÑ ‰ðð ˆq‚yØ �‰ ˆà �Š �u ’zÜÐà €FÙ Ø�a‘%˜1‘*˜a !™e Q¨¡Uˆaˆ1ˆØ�qŠjØ �Q‰JˆEؘ˜A™‘; !Ñ# Q¨¡UˆqˆAð�q‹jð � ‰ �d˜2 ™6‘lÔ#ò ô �‹=ÐrMcó—t|g|«S)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)Úvaluer`s rKr/r/¼s€ô �%�˜(Ó #Ð#rMc#óàK—t|«ddd…}t|«}tdg|¬«|z}t|t d|dz ««D]!}|j |«t ||«–—Œ#y­w)aBConvolve the iterable *signal* with the iterable *kernel*. >>> signal = (1, 2, 3, 4, 5) >>> kernel = [3, 2, 1] >>> list(convolve(signal, kernel)) [3, 8, 14, 20, 26, 14, 5] Note: the input arguments are not interchangeable, as the *kernel* is immediately consumed and stored. NrÝrrXr–)rlr\rrrr¼Ú_sumprod)ÚsignalÚkernelrQÚwindowrIs rKrrËskèø€ô�6‹]™4˜R˜4Ñ €FÜ ˆF‹ €AÜ �A�3˜qÔ ! AÑ %€FÜ �6œ6 ! Q¨¡UÓ+Ö ,ˆØ� ‰ �aÔÜ�v˜vÓ&Ó&ñ-ùs‚A,A.cóZ‡‡‡—t‰«ŠgŠˆˆˆfd„}t‰‰«}|«|fS)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. c3óZ•K—‰D]!}‰|«r|–—Œ‰j|«yy­wrH)r¼)Úelemr›Ú predicateÚ transitions €€€rKÚ true_iteratorz'before_and_after..true_iteratoròs.øèø€ÛˆDÙ˜ŒØ“ à×!Ñ! $Ô'Ùñ ùsƒ(+)r]r)rír›rïÚremainder_iteratorrîs`` @rKrrás5ú€ô ˆb‹€BØ€Jöô˜z¨2Ó.Ðá ‹?Ð.Ð .Ð.rMc#ó^K—tt|««D]\\}}\}}|||f–—Œy­w)z�Return overlapping triplets from *iterable*. >>> list(triplewise('ABCDE')) [('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E')] Nr})rRrzÚ_r{rás rKrArAs3èø€ô#¤8¨HÓ#5Ö6‰‰ˆˆA‘��AØ��Aˆg‹ ñ7ùs‚+-c#ó K—t|«}tt||dz «|¬«}|D] }|j|«t |«–—Œ"y­w)aYReturn 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`. r–rXN)r]rr r¼rl)rRrQr›rérIs rKr9r9 sHèø€ô ˆh‹€BÜ ”6˜"˜a !™eÓ$¨QÔ /€FÛ ˆØ� ‰ �aÔÜ�F‹mÓñùs‚A Ac ó¾—t|«}tttt t |«dz«d««}t tjt|«|«S)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. r–é) rOrÚslicerr¸r\rTrnÚgetitemr)rRÚseqÚslicess rKr:r:!sF€ô ˆx‹.€CÜ ”UœL¬¬s°3«x¸!©|Ó)<¸aÓ@Ó A€FÜ Œx×Ѥ¨£¨fÓ 5Ð5rMcó’—ttd«ttj|««}t t t|dg««S)zÌCompute a polynomial's coefficients from its roots. >>> roots = [5, -4, 3] # (x - 5) * (x + 4) * (x - 3) >>> polynomial_from_roots(roots) # x^3 - 4 * x^2 - 17 * x + 60 [1, -4, -17, 60] r–)ryrrTrnÚnegrOrr)ÚrootsÚfactorss rKr,r,/s5€ô”&˜“)œS¤§¡¨uÓ5Ó6€GÜ ””x ¨1¨#Ó.Ó /Ð/rMc#óöK—t|dd«}|€0t|||«}t||«D]\}}||us||k(sŒ|–—Œy|€ t|«n|}|dz } |||dz|«x}–—Œ#t$rYywxYw­w)aåYield the index of each place in *iterable* that *value* occurs, beginning with index *start* and ending before index *stop*. See :func:`locate` for a more general means of finding the indexes associated with particular values. >>> 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] ràNr–)Úgetattrr r—r\r¡)rRrärVÚstopÚ seq_indexr›ršrÃs rKr"r":s£èø€ô˜ '¨4Ó0€IØÐä �H˜e TÓ *ˆÜ# B¨Ö.‰JˆAˆwؘ%Ñ 7¨eÓ#3Ø“ñ/ð !%  Œs�8Œ}°$ˆØ �A‰Iˆð ØÙ% e¨Q°©U°DÓ9Ð9�qÒ:ðøäò Ù ð üs(‚8A9»A9ÁA*Á* A6Á3A9Á5A6Á6A9c #ójK—|dkDrd–—d}td«|dzz}tj|«dz}t|d||«D]Q}t|d|||z«Ed{–—†t t t ||z|||z«««|||z|||z…<||z}ŒSt|d|«Ed{–—†y7ŒR7Œ­w)zdYield the primes less than n. >>> list(sieve(30)) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] rõr¬)rr–r–N)Ú bytearrayÚmathÚisqrtr"Úbytesr\r¸)rQrVÚdataÚlimitr±s rKr8r8ZsÇèø€ð  ˆ1‚uØŠØ €EÜ �VÓ   Q¡Ñ '€DÜ �J‰J�q‹M˜AÑ €EÜ ˜˜a ¨Ö .ˆÜ˜d A u¨a°!©eÓ4×4Ð4Ü"'¬¬E°!°a±%¸¸AÀ¹EÓ,BÓ(CÓ"DˆˆQ�‰U�Q˜˜Q™Ð ÑØ�A‘‰ð/ô˜$  5Ó)×)Ñ)ð 5øð*ús%‚AB3ÁB/ÁA B3Â)B1Â*B3Â1B3Fc#óàK—|dkr td«‚t|«}tt||««x}r8|rt |«|k7r td«‚|–—tt||««x}rŒ7yy­w)aŽBatch 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`. r–zn must be at least onezbatched(): incomplete batchN)r¡r]rlr r\)rRrQrEr›Úbatchs rKÚ_batchedr lspèø€ð ˆ1‚uÜÐ1Ó2Ð2Ü ˆh‹€BÜœ  A›Ó'Ð 'ˆ%Ð 'Ù ”c˜%“j A’oÜÐ:Ó;Ð ;ØŠ ôœ  A›Ó'Ð 'ˆ%Ó 'ùs ‚A)A.Á,A.i¢ )rcó—t|||¬«S)NrD)Úitertools_batched)rRrQrEs rKrrƒs€Ü  ¨1°VÔ<Ð>> 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. )Ú _zip_strict©r›s rKr@r@Œs€ô ˜Ð ÐrMcó@—ttj|«|«S)zÆReshape the 2-D input *matrix* to have a column count given by *cols*. >>> matrix = [(0, 1), (2, 3), (4, 5)] >>> cols = 3 >>> list(reshape(matrix, cols)) [(0, 1, 2), (3, 4, 5)] )rrrk)ÚmatrixÚcolss rKr1r1˜s€ô ”5×&Ñ& vÓ.°Ó 5Ð5rMc óx—t|d«}tttt |t |«««|«S)zÚ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. r)r\rrrærr@)Úm1Úm2rQs rKr#r#£s0€ô ˆBˆq‰E‹ €AÜ ”7œ8¤W¨R´¸2³Ó%?Ó@À!Ó DÐDrMc#ó¤K—ttj|«dz«D]}||zrŒ |–—||z}|dk(ry||zsŒŒ |dkDr|–—yy­w)zTYield the prime factors of n. >>> list(factor(360)) [2, 2, 2, 3, 3, 5] r–N)r8rr)rQÚprimes rKrr°s_èø€ô ”t—z‘z !“} qÑ(Ö)ˆØ�e“)ØŠKØ �%‰KˆAØ�AŠvÙð �e”)ð*ð  ˆ1‚uØ‹ð ùs‚*A­AÁ Ac óœ—t|«}|dk(r|dzSttt|«t t |«««}t ||«S)zÜEvaluate a polynomial at a specific value. Example: evaluating 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 r)r\rTÚpowrÚreversedr¸ræ)Ú coefficientsrIrQÚpowerss rKr+r+ÀsF€ô ˆLÓ€A؈A‚vØ�1‰uˆ Ü ””f˜Q“i¤¬%°«(Ó!3Ó 4€FÜ �L &Ó )Ð)rMcó$—tt|«ŽS)zfReturn the sum of the squares of the input values. >>> sum_of_squares([10, 20, 30]) 1400 )rærrs rKr;r;Ñs€ô ”S˜“WÐ ÐrMcóŠ—t|«}ttd|««}tt t j ||««S)aCompute the first derivative of a polynomial. Example: evaluating the derivative of x^3 - 4 * x^2 - 17 * x + 60 >>> coefficients = [1, -4, -17, 60] >>> derivative_coefficients = polynomial_derivative(coefficients) >>> derivative_coefficients [3, -8, -17] r–)r\rr¸rOrTrnro)rrQrs rKr-r-Ús6€ô ˆLÓ€AÜ ”e˜A˜q“kÓ "€FÜ ””H—L‘L ,°Ó7Ó 8Ð8rMcóN—tt|««D] }||z|dz z}Œ|S)z�Return the count of natural numbers up to *n* that are coprime with *n*. >>> totient(9) 6 >>> totient(12) 4 r–)rCr)rQr±s rKr?r?és0€ôœV A›YÖ 'ˆØ �‰F�a˜!‘eÑ ‰ð(ð €HrM)rrH)rŸN)NN)rN)\Ú__doc__rrnÚ collectionsrÚcollections.abcrÚ functoolsrrÚ itertoolsrrr r r r r rrrrrÚrandomrrrÚsysrÚ__all__Úobjectr�ryrr˜rÿrær>r<r=rr%rr­r0r(r'r$rrr6r|r)rÚ ImportErrorr¡r�r”r�r r7r*r.rBrCr!rr5r4r3r2r&r/rrrAr9r:r,r"r8r rr r@r1r#rr+r;r-r?rÈrMrKÚr+süðñó ÛåÝ!ß%÷ ÷ ÷ ó ÷-Ñ,Ýò. €ñ` ‹(€ð,ÙˆtÕñ˜# dÔ+€Kñ �4˜Ñ$AÓ B€ò %ó 'ò$3ó$%+óP 4ò 0ð!ó$ò )ð €ò;ò.ò ,ó.ò6 ð )Ý8ò ,ð!×(Ñ(€HÔô˜Jôòò /ó %=òP2ò.Dò8Nó.*óZ Jó ó>1ð("#ô1ó("ò$ +ò +ò"'òT $ò'ò,/òBòò( 6ò0óò@*ð$%*ôð(�ÒÝ6à',õ=ð€Gà×&Ñ&€G„Oò ò6ò Eò ò *ò"ò 9ó  øðeòØ‚KðûðLòØ‚Hðús$Á E Â%E-Å E*Å)E*Å-E7Å6E7