# cycle('ABCD') -> A B C D A B C D A B C D. Roughly equivalent to: def cycle(iterable): When the iterable is exhausted, return elements from the saved copy. Make an iterator returning elements from the iterable and saving a copy of each. The number of items returned is (n+r-1)! / r! / (n-1)! when n > 0.Ĭhanged in version 2.7: added step argument and allowed non-integer arguments. The code for combinations_with_replacement() can be also expressed as a subsequence of product() after filtering entries where the elements are not in sorted order (according to their position in the input pool): def combinations_with_replacement(iterable, r):įor indices in product(range(n), repeat=r): # combinations_with_replacement('ABC', 2) -> AA AB AC BB BC CC Roughly equivalent to: def combinations_with_replacement(iterable, r): So if the input elements are unique, the generated combinations will also be unique. Return r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.Įlements are treated as unique based on their position, not on their value. The number of items returned is n! / r! / (n-r)! when 0 n. The code for combinations() can be also expressed as a subsequence of permutations() after filtering entries where the elements are not in sorted order (according to their position in the input pool): def combinations(iterable, r):įor indices in permutations(range(n), r): # combinations('ABCD', 2) -> AB AC AD BC BD CD Roughly equivalent to: def combinations(iterable, r): So if the input elements are unique, there will be no repeat values in each combination. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.Įlements are treated as unique based on their position, not on their value. Return r length subsequences of elements from the input iterable.Ĭombinations are emitted in lexicographic sort order. Roughly equivalent to: def from_iterable(iterables): Gets chained inputs from a single iterable argument that is evaluated lazily. Roughly equivalent to: def chain(*iterables):Ĭlassmethod om_iterable(iterable)Īlternate constructor for chain(). Used for treating consecutive sequences as a single sequence. Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Some provide streams of infinite length, so they should only be accessed by functions or loops that truncate the stream. The following module functions all construct and return iterators. R-length tuples, in sorted order, with repeated elementsĪA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD R-length tuples, in sorted order, no repeated elements R-length tuples, all possible orderings, no repeated elements Izip_longest('ABCD', 'xy', fillvalue='-') -> Ax By C- D-Ĭartesian product, equivalent to a nested for-loop It1, it2, … itn splits one iterator into n Ifilter(lambda x: x%2, range(10)) -> 1 3 5 7 9Įlements of seq where pred(elem) is false Sub-iterators grouped by value of keyfunc(v) Iterators terminating on the shortest input sequence:Ĭompress('ABCDEF', ) -> A C E F For example, the multiplication operator can be mapped across two vectors to form an efficient dot-product: sum(imap(operator.mul, vector1, vector2)).Įlem, elem, elem, … endlessly or up to n times These tools and their built-in counterparts also work well with the high-speed functions in the operator module. The same effect can be achieved in Python by combining imap() and count() to form imap(f, count()). Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.įor instance, SML provides a tabulation tool: tabulate(f) which produces a sequence f(0), f(1). The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Each has been recast in a form suitable for Python. This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |