[Python-ideas] proposed sequence method: index_subseq()

Steven D'Aprano steve at pearwood.info
Wed Aug 28 05:11:53 CEST 2013


On 28/08/13 06:21, Terry Reedy wrote:
> On 8/27/2013 1:34 PM, Steven D'Aprano wrote:
>> On 28/08/13 00:57, Jess Austin wrote:
>>> Recently I've repeatedly needed to check whether a particular sequence
>>> occurred as a "subsequence" of another. I think this could be a general
>>> requirement, so I'd like to ask if anyone else agrees.
>
> I think the need is too specialized for the stdlib.

This is exactly what strings and bytes do, so it clearly isn't that specialised. The only question is whether it is worth generalizing to sequences other than strings and bytes.


[...]
>> I dislike that. The end and step parts of the slice are redundant: end
>> is easily calculated as just start + len(subseq), and step will always
>> be None. A more familiar, and obvious, functionality is to return the
>> starting index, and then either raise an exception if not found, or
>> return some sentinel value (not -1 since it can be used as an index in
>> error).
>
> There are subsequence algorithms that see, for instance, 1,4,7 as a subsequence of 0,1,2,3,4,5,6,7,8.

That's fine. They can invent their own API, but I reckon that returning a slice will still be the wrong thing to do. Consider (1, 3, 8) as a subsequence of (1, 2, 3, 4, 5, 6, 7, 8), what slice should be returned?


-- 
Steven


More information about the Python-ideas mailing list