Skip to content

Commit a77de58

Browse files
authored
Add recipe for subslices (GH-31095)
1 parent f5ebec4 commit a77de58

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Doc/library/itertools.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,12 @@ which incur interpreter overhead.
893893
yield from it
894894
return true_iterator(), remainder_iterator()
895895

896+
def subslices(seq):
897+
"Return all contiguous non-empty subslices of a sequence"
898+
# subslices('ABCD') --> A AB ABC ABCD B BC BCD C CD D
899+
slices = starmap(slice, combinations(range(len(seq) + 1), 2))
900+
return map(operator.getitem, repeat(seq), slices)
901+
896902
def powerset(iterable):
897903
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
898904
s = list(iterable)

0 commit comments

Comments
 (0)