获取多行字符串中单词“print”的索引

2024-03-29 14:33:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找到多行文本中所有单词的索引:“print”。但也存在一些问题,这些问题是:

  1. 如果一行中有两个打印,代码将两次返回单词“print”的相同索引
  2. 它无法在同一行中找到第二个“打印”的索引,但会将第一个“打印”的索引打印两次。 我的代码是:
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}')
                        

当我运行它时,会打印:

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

您可以看到,在结果中,索引是重复的。这是文本列表:

>>> 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']]
>>>

文本列表中的每个列表都是文本中的一行。有三行,因此在文本列表中有三个列表。如何获取第一行中第二个“打印”的索引以及第三行中第二个和第三个“打印”的索引?您可以看到,它只返回第一行和第三行中第一个“print”的索引


Tags: ofthetextin文本indexifline
3条回答

strings已经有了一个index方法来查找子字符串,您可以提供额外的参数来查找给定子字符串的下一个副本的下一个副本

>>> text = '''print is print as
it is the function an
print is print and not print
'''
>>> text.index("print")
0
>>> text.index("print",1)
9
>>> text.index("print",10)
40
>>> text.index("print",41)
49
>>> text.index("print",50)
63
>>> text.index("print",64)
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    text.index("print",64)
ValueError: substring not found
>>> 
import re

text = '''print is print as
it is the function an
print is print and not print
'''

for line_number, line in enumerate(text.split('\n')):
    occurrences = [m.start() for m in re.finditer('print', line)]

    if occurrences:
        for occurrence in occurrences:
            print('Found `print` at character %d on line %d' % (occurrence, line_number + 1))

->

Found `print` at character 0 on line 1
Found `print` at character 9 on line 1
Found `print` at character 0 on line 3
Found `print` at character 9 on line 3
Found `print` at character 23 on line 3

您可以使用正则表达式:

import re

text = '''print is print as
it is the function an
print is print and not print
'''

for i in re.finditer("print", text):
    print(i.start())

# OR AS A LIST

[i.start() for i in re.finditer("print", text)]

相关问题 更多 >