• Home
  • Raw
  • Download

Lines Matching +full:repeat +full:- +full:element

1 :mod:`itertools` --- Functions creating iterators for efficient looping
18 --------------
33 These tools and their built-in counterparts also work well with the high-speed
35 operator can be mapped across two vectors to form an efficient dot-product:
44 …tart, start+step, start+2*step, ... ``count(10) --> 10 11 12 13 14 ...``
45 …1, ... plast, p0, p1, ... ``cycle('ABCD') --> A B C D A B C D ...…
46 :func:`repeat` elem [,n] elem, elem, elem, ... endlessly or up to n times …
54 … p0, p0+p1, p0+p1+p2, ... ``accumulate([1,2,3,4,5]) --> 1 3 6 10 15``
55 … p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') --> A B C D E F``
56 …... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) --> A B C D E F``
57 …[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F``
58 …n], seq[n+1], starting when pred fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1``
59 … of seq where pred(elem) is false ``filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8``
60 :func:`groupby` iterable[, key] sub-iterators grouped by value of k…
61 … elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) --> C D E F G``
62 … (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') --> AB BC CD DE EF FG``
63 …seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000``
64 …q[0], seq[1], until pred fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4``
66 … (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-``
74 … p, q, ... [repeat=1] cartesian product, equivalent to a ne…
75 :func:`permutations` p[, r] r-length tuples, all po…
76 :func:`combinations` p, r r-length tuples, in sor…
77 :func:`combinations_with_replacement` p, r r-length tuples, in sor…
83 ``product('ABCD', repeat=2)`` ``AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD``
90 .. _itertools-functions:
93 ------------------
115 has one more element than the input iterable.
121 # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
122 # accumulate([1,2,3,4,5], initial=100) --> 100 101 103 106 110 115
123 # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
132 for element in it:
133 total = func(total, element)
150 >>> cashflows = [1000, -90, -90, -90, -90]
173 # chain('ABC', 'DEF') --> A B C D E F
175 for element in it:
176 yield element
185 # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
187 for element in it:
188 yield element
206 # combinations('ABCD', 2) --> AB AC AD BC BD CD
207 # combinations(range(4), 3) --> 012 013 023 123
216 if indices[i] != i + n - r:
222 indices[j] = indices[j-1] + 1
236 The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n``
255 # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
264 if indices[i] != n - 1:
268 indices[i:] = [indices[i] + 1] * (r - i)
278 for indices in product(range(n), repeat=r):
282 The number of items returned is ``(n+r-1)! / r! / (n-1)!`` when ``n > 0``.
290 have a corresponding element in *selectors* that evaluates to ``True``.
295 # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
308 # count(10) --> 10 11 12 13 14 ...
309 # count(2.5, 0.5) --> 2.5 3.0 3.5 ...
320 Added *step* argument and allowed non-integer arguments.
329 # cycle('ABCD') --> A B C D A B C D A B C D ...
331 for element in iterable:
332 yield element
333 saved.append(element)
335 for element in saved:
336 yield element
345 is true; afterwards, returns every element. Note, the iterator does not produce
347 start-up time. Roughly equivalent to::
350 # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
366 # filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
377 The *key* is a function computing a key value for each element. If not
379 the element unchanged. Generally, the iterable needs to already be sorted on
403 # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
404 # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
438 non-zero, then elements from the iterable are skipped until start is reached.
450 multi-line report may list a name field on every third line).
455 # islice('ABCDEFG', 2) --> A B
456 # islice('ABCDEFG', 2, 4) --> C D
457 # islice('ABCDEFG', 2, None) --> C D E F G
458 # islice('ABCDEFG', 0, None, 2) --> A C E G
466 for i, element in zip(range(start), iterable):
470 for i, element in enumerate(iterable):
472 yield element
476 for i, element in zip(range(i + 1, stop), iterable):
484 The number of 2-tuples in the output iterator will be one fewer than the
491 # pairwise('ABCDEFG') --> AB BC CD DE EF FG
504 of the *iterable* and all possible full-length permutations
518 # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
519 # permutations(range(3)) --> 012 021 102 120 201 210
526 cycles = list(range(n, n-r, -1))
530 cycles[i] -= 1
533 cycles[i] = n - i
536 indices[i], indices[-j] = indices[-j], indices[i]
550 for indices in product(range(n), repeat=r):
554 The number of items returned is ``n! / (n-r)!`` when ``0 <= r <= n``
557 .. function:: product(*iterables, repeat=1)
561 Roughly equivalent to nested for-loops in a generator expression. For example,
564 The nested loops cycle like an odometer with the rightmost element advancing
570 repetitions with the optional *repeat* keyword argument. For example,
571 ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
576 def product(*args, repeat=1):
577 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
578 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
579 pools = [tuple(pool) for pool in args] * repeat
590 .. function:: repeat(object[, times])
597 def repeat(object, times=None):
598 # repeat(10, 3) --> 10 10 10
606 A common use for *repeat* is to supply a stream of constant values to *map*
611 >>> list(map(pow, range(10), repeat(2)))
619 "pre-zipped").
626 # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
637 # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
651 :abbr:`FIFO (first-in, first-out)` queue)::
685 iterables are of uneven length, missing values are filled-in with *fillvalue*.
689 # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
700 num_active -= 1
703 iterators[i] = repeat(fillvalue)
714 .. _itertools-recipes:
717 -----------------
728 well as with the built-in itertools such as ``map()``, ``filter()``,
737 the `more-itertools project <https://pypi.org/project/more-itertools/>`_ found
740 python -m pip install more-itertools
747 "vectorized" building blocks over the use of for-loops and :term:`generator`\s
763 # prepend(1, [2, 3, 4]) --> 1 2 3 4
772 # tail(3, 'ABCDEFG') --> E F G
776 "Advance the iterator n-steps ahead. If n is None, consume entirely."
779 # feed the entire iterator into a zero-length deque
800 return chain.from_iterable(repeat(tuple(iterable), n))
804 # batched('ABCDEFG', 3) --> ABC DEF G
812 "Collect data into non-overlapping fixed-length chunks or blocks"
813 # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx
814 # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError
815 # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF
832 # sum_of_squares([10, 20, 30]) -> 1400
837 # transpose([(1, 2, 3), (11, 22, 33)]) --> (1, 11) (2, 22) (3, 33)
842 # matmul([(7, 5), (3, 5)], [[2, 5], [7, 9]]) --> (49, 80), (41, 60)
847 # See: https://betterexplained.com/articles/intuitive-convolution/
848 # convolve(data, [0.25, 0.25, 0.25, 0.25]) --> Moving average (blur)
849 # convolve(data, [1, -1]) --> 1st finite difference (1st derivative)
850 # convolve(data, [1, -2, 1]) --> 2nd finite difference (2nd derivative)
851 kernel = tuple(kernel)[::-1]
854 for x in chain(signal, repeat(0, n-1)):
861 (x - 5) (x + 4) (x - 3) expands to: x³ -4x² -17x + 60
863 # polynomial_from_roots([5, -4, 3]) --> [1, -4, -17, 60]
866 expansion = convolve(expansion, (1, -r))
874 # Evaluate x³ -4x² -17x + 60 at x = 2.5
875 # polynomial_eval([1, -4, -17, 60], x=2.5) --> 8.125
879 powers = map(pow, repeat(x), reversed(range(n)))
884 # iter_index('AABCADEAF', 'A') --> 0 1 4 7
890 i = start - 1
898 i = start - 1
907 # sieve(30) --> 2 3 5 7 11 13 17 19 23 29
918 # factor(99) --> 3 3 11
936 """Repeat calls to func with specified arguments.
941 return starmap(func, repeat(args))
942 return starmap(func, repeat(args, times))
946 # triplewise('ABCDEFG') --> ABC BCD CDE DEF EFG
951 # sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
961 "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
971 num_active -= 1
976 # partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
1010 "Return all contiguous non-empty subslices of a sequence"
1011 # subslices('ABCD') --> A AB ABC ABCD B BC BCD C CD D
1013 return map(operator.getitem, repeat(seq), slices)
1016 "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
1022 # unique_everseen('AAAABBBCCDAABBB') --> A B C D
1023 # unique_everseen('ABBcCAD', str.lower) --> A B c D
1026 for element in filterfalse(seen.__contains__, iterable):
1027 seen.add(element)
1028 yield element
1030 # a faster but non-lazy solution is:
1033 for element in iterable:
1034 k = key(element)
1037 yield element
1038 # For use cases that allow the last matching element to be returned,
1039 # a faster but non-lazy solution is:
1044 "List unique elements, preserving order. Remember only the element just seen."
1045 # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
1046 # unique_justseen('ABBcCAD', str.lower) --> A B c A D
1052 Converts a call-until-exception interface to an iterator interface.
1058 iter_except(d.popitem, KeyError) # non-blocking dict iterator
1059 iter_except(d.popleft, IndexError) # non-blocking deque iterator
1061 iter_except(s.pop, KeyError) # non-blocking set iterator
1081 # first_true([a,b,c], x) --> a or b or c or x
1082 # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
1096 c, n, r = c*r//n, n-1, r-1
1098 index -= c
1099 c, n = c*(n-r)//n, n-1
1100 result.append(pool[-1-n])
1118 >>> for cube in map(operator.pow, range(1,4), repeat(3)):
1149 >>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
1232 >>> list(convolve(data, [1, -1]))
1233 [20, 20, -16, 8, -12, 8, -12, -16]
1234 >>> list(convolve(data, [1, -2, 1]))
1235 [20, 0, -36, 24, -20, 20, -20, -4, 16]
1237 >>> polynomial_from_roots([5, -4, 3])
1238 [1, -4, -17, 60]
1239 >>> factored = lambda x: (x - 5) * (x + 4) * (x - 3)
1240 >>> expanded = lambda x: x**3 -4*x**2 -17*x + 60
1241 >>> all(factored(x) == expanded(x) for x in range(-10, 11))
1450 ... for i in range(-len(seq), 0):