![]() compress (shapes, selections ) > for each in result. compress (data, selectors )Įxample: > shapes = > selections = > result = itertools. # red # orange # yellow # green # blue # circle # triangle # square # pentagon compress()įilters one iterable with another. chain (colors, shapes ) > for each in result. chain ( *iterables )Įxample: > colors = > shapes = > result = itertools. Take a series of iterables and return them as one long iterable. When reached the end of the iterable it start over again from the beginning. # red # orange # yellow # green # blue # violet # red # orange cycle (iterable )Įxample: > colors = > for color in itertools. This function cycles through an iterator endlessly. count (start = 0, step = 1 )Įxample: > for i in itertools. Makes an iterator that returns evenly spaced values starting with number start. combinations_with_replacement (shapes, 2 ) > for each in result. combinations_with_replacement (iterable, r )Įxample: > shapes = > result = itertools. Just like combinations(), but allows individual elements to be repeated more than once. # ('circle', 'triangle') # ('circle', 'square') # ('triangle', 'square') combinations_with_replacement() combinations (shapes, 2 ) > for each in result. combinations (iterable, r )Įxample: > shapes = > result = itertools. This will create all the unique combination that have r members. If no function is designated the items will be summed: 5 5 + 2 = 7 7 + 6 = 13 13 + 4 = 17 17 + 5 = 22 22 + 9 = 31 31 + 1 = 32 combinations() Passing a function is optional: > data = > result = itertools. ![]() The operator.mul takes two numbers and multiplies them: operator. accumulate (iterable )Įxample: > data = > result = itertools. Makes an iterator that returns the results of a function. ![]() This module is not necessary when using itertools, but needed for some of the examples below. The itertools module comes in the standard library and must be imported. ![]() Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python. The version history can be found in documentation.The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. If you have a problem or suggestion, please file a bug or pull request in this More-itertools is maintained by with help from many others. Several new recipes are available as well: > from more_itertools import chunked > iterable = > list ( chunked ( iterable, 3 )), , ] > from more_itertools import spy > iterable = ( x * x for x in range ( 1, 6 )) > head, iterable = spy ( iterable, n = 3 ) > list ( head ) > list ( iterable ) įor the full listing of functions, see the API documentation. To get started, install the library with pip: pip install more-itertoolsĪre included in the top-level package: > from more_itertools import flatten > iterable = > list ( flatten ( iterable )) We collect additional building blocks, recipes, and routines for working with Python’s itertools library is a gem - you can compose elegant solutionsįor a variety of problems with the functions it provides.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |