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

时间:2016-06-20 16:15:02

标签: python list tuples immutability mutable

我想在python中创建一个空列表,以便稍后我可以通过函数将项添加到其中。但是当我尝试通过功能向它添加项目时,它向我展示了#34; 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, " \
       "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)

2 个答案:

答案 0 :(得分:1)

在表达式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)

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

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

答案 1 :(得分:0)

怎么样:

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