Python. See if a string in one sequence is included in another.

Asked 2 years ago, Updated 2 years ago, 22 views

python

2022-09-22 18:11

1 Answers

Hello, everyone

fori in range(len(seq2)):# get seq2 length and repeat for statement by length

  If (i+2 < len(seq2)): # To prevent an i+2 string with a length of 4 and an index of 5.
    word = seq2[i] + seq2[i+1] + seq2[i+2]  
                          # I'll make a word

    If word in seq2 : # Make sure the word is in the seq2 string!
      print('Success:', word)

I'm going to find these three~

fori in range (len(seq2)):
   if (i+2 < len(seq2)):
    word = seq2[i] + seq2[i+1] + seq2[i+2]
    print(word)

All I need to do is check if there is or not You can use if in grammar.

(Additional 1)

for i in range(len(seq2)):
  if (i+2 < len(seq2)):
    word = seq2[i] + seq2[i+1] + seq2[i+2]
    print(word)
    index = seq1.find(word)
    if index != -1 :
      print(the word f' you're looking for {word} is in [{index}:{index+len(word)-1}]')

(Additional 2)

Actually... LEA, PGV, DLK... There are two words that come out together. I have to write a regular expression. Please keep that in mind

import re

for i in range(len(seq2)):
  if (i+2 < len(seq2)):
    word = seq2[i] + seq2[i+1] + seq2[i+2]
    index = seq1.find(word)
    if index != -1:
      indexes = [w.start() for w in re.finditer(word, seq1)]
      print(the word you're looking for {word} in {indexes})


2022-09-22 18:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.