如何在 python 中创建一个空的可变列表,以便以后可以添加该列表项?

How to create an empty mutable list in python, so that list item can be added later?

我想在 python 中创建一个空列表,以便稍后可以通过函数向其中添加项目。但是当我尝试通过函数向其中添加项目时,它显示 "TypeError: Can't convert 'tuple' object to str implicitly"。为什么会收到这个?

page = "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, " \
       "or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't " \
       "anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, " \
       "making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence " \
       "structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, " \
       "or non-characteristic words etc."

find_word = "the"
word_positions = []
pos = 0

while page.find(find_word) != -1:
        word_positions.append(page.find((find_word, pos)))
        pos = pos + len(find_word)

print(word_positions)

在表达式 word_positions.append(page.find((find_word, pos))) 中,page.find((find_word, pos))tuple 传递给 page.find,但 page.find 期望第一个参数是字符串(要查找的单词)。你想要:

page.find(find_word, pos)

(注意我去掉了一组括号)


您的代码中还有其他一些逻辑错误。首先,您的循环可能会永远持续下去,因为 page.find(find_word) 如果它第一次找到某些东西,它总会找到一些东西。将其更改为:

while page.find(find_word, pos) != -1:

其次,您最终会在列表中找到重复项:

pos = pos + len(find_word)

找到的单词数量与您希望在什么位置找到它们无关。你可能想要:

pos = word_positions[-1] + 1

因为您想在上次找到的项目后立即继续查找。


最后,使用 re 几乎可以轻松完成此任务。 (你甚至不必写一个正则表达式,因为你正在寻找一个字面词!):

import re
word_positions = []
for match in re.finditer(find_word, page):
    word_positions.append(match.start())

print(word_positions)

请注意,这也可以写在 1 行中作为列表理解:

word_positions = [m.start() for m in re.finditer(find_word, page)]

怎么样:

import re

page = "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, " \
       "or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't " \
       "anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, " \
       "making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence " \
       "structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, " \
       "or non-characteristic words etc."

find_word = "the"
word_positions = []
pos = 0

for match in re.finditer(find_word, page):
    word_positions.append( (find_word, match.start()) )

print(word_positions)

它输出:

[('the', 68), ('the', 273), ('the', 317), ('the', 341), ('the', 371), ('the', 443), ('the', 471), ('the', 662)]