README.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ==============
  2. More Itertools
  3. ==============
  4. .. image:: https://coveralls.io/repos/github/erikrose/more-itertools/badge.svg?branch=master
  5. :target: https://coveralls.io/github/erikrose/more-itertools?branch=master
  6. Python's ``itertools`` library is a gem - you can compose elegant solutions
  7. for a variety of problems with the functions it provides. In ``more-itertools``
  8. we collect additional building blocks, recipes, and routines for working with
  9. Python iterables.
  10. ----
  11. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  12. | Grouping | `chunked <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.chunked>`_, |
  13. | | `sliced <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.sliced>`_, |
  14. | | `distribute <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.distribute>`_, |
  15. | | `divide <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.divide>`_, |
  16. | | `split_at <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.split_at>`_, |
  17. | | `split_before <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.split_before>`_, |
  18. | | `split_after <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.split_after>`_, |
  19. | | `bucket <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.bucket>`_, |
  20. | | `grouper <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.grouper>`_, |
  21. | | `partition <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.partition>`_ |
  22. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  23. | Lookahead and lookback | `spy <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.spy>`_, |
  24. | | `peekable <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.peekable>`_, |
  25. | | `seekable <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.seekable>`_ |
  26. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | Windowing | `windowed <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.windowed>`_, |
  28. | | `stagger <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.stagger>`_, |
  29. | | `pairwise <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.pairwise>`_ |
  30. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | Augmenting | `count_cycle <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.count_cycle>`_, |
  32. | | `intersperse <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.intersperse>`_, |
  33. | | `padded <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.padded>`_, |
  34. | | `adjacent <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.adjacent>`_, |
  35. | | `groupby_transform <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.groupby_transform>`_, |
  36. | | `padnone <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.padnone>`_, |
  37. | | `ncycles <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.ncycles>`_ |
  38. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | Combining | `collapse <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.collapse>`_, |
  40. | | `sort_together <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.sort_together>`_, |
  41. | | `interleave <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.interleave>`_, |
  42. | | `interleave_longest <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.interleave_longest>`_, |
  43. | | `collate <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.collate>`_, |
  44. | | `zip_offset <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.zip_offset>`_, |
  45. | | `dotproduct <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.dotproduct>`_, |
  46. | | `flatten <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.flatten>`_, |
  47. | | `roundrobin <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.roundrobin>`_, |
  48. | | `prepend <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.prepend>`_ |
  49. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | Summarizing | `ilen <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.ilen>`_, |
  51. | | `first <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.first>`_, |
  52. | | `last <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.last>`_, |
  53. | | `one <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.one>`_, |
  54. | | `unique_to_each <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.unique_to_each>`_, |
  55. | | `locate <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.locate>`_, |
  56. | | `rlocate <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.rlocate>`_, |
  57. | | `consecutive_groups <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.consecutive_groups>`_, |
  58. | | `exactly_n <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.exactly_n>`_, |
  59. | | `run_length <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.run_length>`_, |
  60. | | `map_reduce <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.map_reduce>`_, |
  61. | | `all_equal <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.all_equal>`_, |
  62. | | `first_true <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.first_true>`_, |
  63. | | `nth <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.nth>`_, |
  64. | | `quantify <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.quantify>`_ |
  65. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | Selecting | `islice_extended <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.islice_extended>`_, |
  67. | | `strip <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.strip>`_, |
  68. | | `lstrip <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.lstrip>`_, |
  69. | | `rstrip <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.rstrip>`_, |
  70. | | `take <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.take>`_, |
  71. | | `tail <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.tail>`_, |
  72. | | `unique_everseen <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertoo ls.unique_everseen>`_, |
  73. | | `unique_justseen <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.unique_justseen>`_ |
  74. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  75. | Combinatorics | `distinct_permutations <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.distinct_permutations>`_, |
  76. | | `circular_shifts <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.circular_shifts>`_, |
  77. | | `powerset <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.powerset>`_, |
  78. | | `random_product <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.random_product>`_, |
  79. | | `random_permutation <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.random_permutation>`_, |
  80. | | `random_combination <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.random_combination>`_, |
  81. | | `random_combination_with_replacement <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.random_combination_with_replacement>`_, |
  82. | | `nth_combination <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.nth_combination>`_ |
  83. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | Wrapping | `always_iterable <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_iterable>`_, |
  85. | | `consumer <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.consumer>`_, |
  86. | | `with_iter <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.with_iter>`_, |
  87. | | `iter_except <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.iter_except>`_ |
  88. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | Others | `replace <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.replace>`_, |
  90. | | `numeric_range <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.numeric_range>`_, |
  91. | | `always_reversible <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.always_reversible>`_, |
  92. | | `side_effect <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.side_effect>`_, |
  93. | | `iterate <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.iterate>`_, |
  94. | | `difference <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.difference>`_, |
  95. | | `make_decorator <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.make_decorator>`_, |
  96. | | `SequenceView <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.SequenceView>`_, |
  97. | | `consume <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.consume>`_, |
  98. | | `accumulate <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.accumulate>`_, |
  99. | | `tabulate <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.tabulate>`_, |
  100. | | `repeatfunc <https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.repeatfunc>`_ |
  101. +------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  102. Getting started
  103. ===============
  104. To get started, install the library with `pip <https://pip.pypa.io/en/stable/>`_:
  105. .. code-block:: shell
  106. pip install more-itertools
  107. The recipes from the `itertools docs <https://docs.python.org/3/library/itertools.html#itertools-recipes>`_
  108. are included in the top-level package:
  109. .. code-block:: python
  110. >>> from more_itertools import flatten
  111. >>> iterable = [(0, 1), (2, 3)]
  112. >>> list(flatten(iterable))
  113. [0, 1, 2, 3]
  114. Several new recipes are available as well:
  115. .. code-block:: python
  116. >>> from more_itertools import chunked
  117. >>> iterable = [0, 1, 2, 3, 4, 5, 6, 7, 8]
  118. >>> list(chunked(iterable, 3))
  119. [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
  120. >>> from more_itertools import spy
  121. >>> iterable = (x * x for x in range(1, 6))
  122. >>> head, iterable = spy(iterable, n=3)
  123. >>> list(head)
  124. [1, 4, 9]
  125. >>> list(iterable)
  126. [1, 4, 9, 16, 25]
  127. For the full listing of functions, see the `API documentation <https://more-itertools.readthedocs.io/en/latest/api.html>`_.
  128. Development
  129. ===========
  130. ``more-itertools`` is maintained by `@erikrose <https://github.com/erikrose>`_
  131. and `@bbayles <https://github.com/bbayles>`_, with help from `many others <https://github.com/erikrose/more-itertools/graphs/contributors>`_.
  132. If you have a problem or suggestion, please file a bug or pull request in this
  133. repository. Thanks for contributing!