2021年1月29日星期五

Getting the index of the word 'print' in a multiline string

I am trying to find the index of all the word: 'print' in a multi line text. But there are some problems, those are:

  1. The code returns the index same of word 'print' two time if there are two prints in a line.
  2. It is not able to find the index of the second 'print' in the same line, but prints the index of the first 'print' two times. My code is:
text = '''print is print as  it is the function an  print is print and not print  '''    text_list = []    for line in text.splitlines():        #'line' represents each line in the multiline string      text_list.append([])        for letter in line:          #Append the letter of each line in a list inside the the text_list          text_list[len(text_list)-1].append(letter)    for line in text_list:      for letter in line:            #check if the letter is after 'p' is 'r' and after that 'i' and then 'n' and at last 't'          if letter == "p":              num = 1                if text_list[text_list.index(line)][line.index(letter)+num] == 'r':                  num += 1                                    if text_list[text_list.index(line)][line.index(letter)+num] == 'i':                      num += 1                        if text_list[text_list.index(line)][line.index(letter)+num] == 'n':                          num += 1                            if text_list[text_list.index(line)][line.index(letter)+num] == 't':                              num += 1                              print(f'index (start,end) = {text_list.index(line)}.{line.index(letter)}, {text_list.index(line)}.{line.index(letter)+num}')                            

when I run it prints:

index (start,end) = 0.0, 0.5 #returns the index of the first 'print' in first line  index (start,end) = 0.0, 0.5 #returns the index of the first 'print' in first line instead of the index of the second print  index (start,end) = 2.0, 2.5 #returns the index of the first 'print' in third line  index (start,end) = 2.0, 2.5 #returns the index of the first 'print' in third line instead of the index of the second print  index (start,end) = 2.0, 2.5 #returns the index of the first 'print' in third line instead of the index of the third print  

you can see that in the result, the index are repeated. This is the text_list:

>>> text_list  [['p', 'r', 'i', 'n', 't', ' ', 'i', 's', ' ', 'p', 'r', 'i', 'n', 't', ' ', 'a', 's'],  ['i', 't', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 'f', 'u', 'n', 'c', 't', 'i', 'o', 'n', ' ', 'a', 'n'],  ['p', 'r', 'i', 'n', 't', ' ', 'i', 's', ' ', 'p', 'r', 'i', 'n', 't', ' ', 'a', 'n', 'd', ' ', 'n', 'o', 't', ' ', 'p', 'r', 'i', 'n', 't']]  >>>  

each list inside the text_list is a line in the text. There are three lines, so there are three lists inside the text_list. How do I get the index of the second 'print' in the first line and the index of second and third 'print' in the third line? You can see that it returns only the index of first 'print' in the first and third line.

https://stackoverflow.com/questions/65964181/getting-the-index-of-the-word-print-in-a-multiline-string January 30, 2021 at 10:51AM

没有评论:

发表评论