[Python-checkins] Add recipe for subslices (GH-31095)

rhettinger webhook-mailer at python.org
Thu Feb 3 03:12:18 EST 2022


https://github.com/python/cpython/commit/a77de58108a89ada49a3af7613e84df436fd147c
commit: a77de58108a89ada49a3af7613e84df436fd147c
branch: 3.10
author: Raymond Hettinger <rhettinger at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2022-02-03T02:12:08-06:00
summary:

Add recipe for subslices (GH-31095)

files:
M Doc/library/itertools.rst

diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 34667561c3cfe..6e1ba3c440124 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -893,6 +893,12 @@ which incur interpreter overhead.
            yield from it
        return true_iterator(), remainder_iterator()
 
+   def subslices(seq):
+       "Return all contiguous non-empty subslices of a sequence"
+       # subslices('ABCD') --> A AB ABC ABCD B BC BCD C CD D
+       slices = starmap(slice, combinations(range(len(seq) + 1), 2))
+       return map(operator.getitem, repeat(seq), slices)
+
    def powerset(iterable):
        "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
        s = list(iterable)



More information about the Python-checkins mailing list