【问题标题】:How to create an empty mutable list in python, so that list item can be added later?如何在python中创建一个空的可变列表,以便以后可以添加列表项?
【发布时间】:2016-06-20 16:15:01
【问题描述】:

我想在 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, " \
       "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)

【问题讨论】:

  • page.find((find_word, pos)) 你得到这个是因为你将一个元组传递给find

标签: python list tuples immutability mutable


【解决方案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)

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

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

【讨论】:

  • 我丢了一组括号。但它没有将单词的位置添加到列表中(word_positions)
【解决方案2】:

怎么样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 2018-03-25
    • 1970-01-01
    相关资源
    最近更新 更多