Finding subsequence (nonconsecutive)

后端 未结 4 924
孤城傲影
孤城傲影 2020-11-29 10:18

If I have string needle and I want to check if it exists contiguously as a substring in haystack, I can use:

if needle in          


        
4条回答
  •  余生分开走
    2020-11-29 10:38

    Using an iterator trick:

    it = iter(haystack)
    all(x in it for x in needle)
    

    This is only a concise version of the same idea presented in another answer.

提交回复
热议问题