【发布时间】: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