如何在python中创建一个空的可变列表,以便以后可以添加列表项?
我想在python中创建一个空列表,以便以后可以通过函数向其中添加项。但当我试图通过函数向其中添加项时,它显示了“TypeError:无法隐式地将'tuple'对象转换为str”。为什么我会得到这个如何在python中创建一个空的可变列表,以便以后可以添加列表项?,python,list,tuples,immutability,mutable,Python,List,Tuples,Immutability,Mutable,我想在python中创建一个空列表,以便以后可以通过函数向其中添加项。但当我试图通过函数向其中添加项时,它显示了“TypeError:无法隐式地将'tuple'对象转换为str”。为什么我会得到这个 page = "There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, "
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\u positions.append(page.find((find\u word,pos))
中,page.find((find\u word,pos))
将一个元组传递给page.find
,但是page.find
期望第一个参数是字符串(要查找的单词)。你想要:
page.find(find_word, pos)
(注意,我删除了一组括号)
代码中还有其他一些逻辑错误。首先,您的循环可能会永远持续下去,因为page.find(find\u word)
如果第一次找到某个内容,它将始终找到该内容。将其更改为:
while page.find(find_word, pos) != -1:
其次,您的列表中会出现以下重复项:
pos = pos + len(find_word)
找到的单词数量与您希望在哪个位置找到它们无关。您可能想要:
pos = word_positions[-1] + 1
因为您希望在最后找到的项目之后立即继续查找
最后,使用re
几乎可以轻松完成此任务。(您甚至不必编写正则表达式,因为您需要的是文字!):
请注意,这也可以写在一行中作为列表:
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)]
page.find((find_-word,pos))
您之所以得到这个结果,是因为您将一个元组传递给了find
。我删除了一组括号。但它并没有将单词的位置添加到列表中(单词的位置)
[('the', 68), ('the', 273), ('the', 317), ('the', 341), ('the', 371), ('the', 443), ('the', 471), ('the', 662)]