How to check if letter in a string appear in another string in the same order

后端 未结 4 1855
天命终不由人
天命终不由人 2021-01-16 08:10

I would love to check if the letters from a text appear in another text in the same order.

text    \"Ce\"
name    \"Arsenic\"
Answer   False


for x in te         


        
4条回答
  •  清歌不尽
    2021-01-16 08:46

    One way to do it would be to avoid iterating through each letter of the word.

    if any(text in name for text in text_collection):
        print(text)
    

    This would check the whole string of text against a string in name. This is assuming that text_collection has more than one entry. Otherwise just use if any(text in name): See the official documentation of any here.

提交回复
热议问题