]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add recipe for subslices (GH-31095)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Thu, 3 Feb 2022 08:12:08 +0000 (02:12 -0600)
committerGitHub <noreply@github.com>
Thu, 3 Feb 2022 08:12:08 +0000 (02:12 -0600)
Doc/library/itertools.rst

index 34667561c3cfe760e05638da2ccef976718d9fe9..6e1ba3c440124d89b37fd0f117a61480bd2cf9d4 100644 (file)
@@ -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)