我需要把这些词变成过去式,在列表中每个词的结尾处加一个d或ed,这取决于该词是否以 "e "结尾。我试图把它们放在第二张过去式单词列表中。
我试图用我已经掌握的基本知识来工作。这在.append行抛出一个错误,说列表索引必须是整数而不是字符串。我有点卡住了,有什么想法吗?
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for i in words:
if i[-1] == 'e':
past_tense.append(words[i] + 'd')
else:
past_tense.append(words[i] + 'ed')
你的治疗 i
如同 words[i]
. i
当你做的时候,是字 for i in words:
. 替换成 i
与 word
. 那么你就不会被索引和单词所迷惑。
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for word in words:
if word[-1] == 'e':
past_tense.append(word + 'd')
else:
past_tense.append(word + 'ed')
print(past_tense)
# ['adopted', 'baked', 'beamed', 'confided', 'grilled', 'planted', 'timed', 'waved', 'wished']
我们也可以直接使用列表理解。
past_tense = [word + 'd' if word[-1] == 'e' else word + 'ed' for word in words]
print(past_tense)
# ['adopted', 'baked', 'beamed', 'confided', 'grilled', 'planted', 'timed', 'waved', 'wished']
for i in words:
这样可以遍历列表中的每一个单词。所以这不是一个整数。i
现在是一个字符串。words[i]
将通过你得到的错误。
工作代码。
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for index,word in enumerate(words):
if word[-1] == 'e':
past_tense.append(words[index] + 'd')
else:
past_tense.append(words[index] + 'ed')
当做 for i in words
事实上 i
将是数组中的一个元素 words
所以a 词
你不需要数组中的索引,只需要数值。
for word in words:
if word[-1] == 'e':
past_tense.append(word + 'd')
else:
past_tense.append(word + 'ed')
利用列表理解。
past_tense = [word + ('d' if word[-1]=='e' else 'ed') for word in words]
让自己对列表理解得心应手。
past_tense = [word + ("d" if word.endswith("e") else "ed")
for word in words]
print(past_tense)
哪种方式产生了
['adopted', 'baked', 'beamed', 'confided', 'grilled', 'planted', 'timed', 'waved', 'wished']
for word in words:
if word.endswith("e"):
past_tense.append(word + "d")
else:
past_tense.append(word + "ed")
或者甚至。
for word in words:
suffix = "d" if word.endswith("e") else "ed"
past_tense.append(word + suffix)
这就是你要找的东西,只是需要做一些调整(见评论)。
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for word in words:
# changed i to word, to reflect what the variable will actually contain.
if word.endswith('e'):
# endswith is a nice string method that makes the code very readable.
past_tense.append(word + 'd')
else:
past_tense.append(word + 'ed')
结果:
>>> print(past_tense)
['adopted', 'baked', 'beamed', 'confided', 'grilled', 'planted', 'timed', 'waved', 'wished']
当你做 for i in words:
你实际上是将'i'设置为单词列表中的一个实际项目(在这种情况下是一个单词),所以你可以看到'i'是一个字符串而不是一个整数。
你的错误来自于这一行。
past_tense.append(words[i] + 'd')
因为,如前所述,你不能用 "i "作为索引,因为它是一个字符串。
所以你的代码必须是。
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for i in words:
if i[-1] == 'e':
past_tense.append(i + 'd')
else:
past_tense.append(i + 'ed')
或者如果你用 for i in range(0,len(words)):
这意味着'i'将从0到词表长度-1。
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for i in range(0,len(words)) :
if words[i][-1] == 'e':
past_tense.append(words[i] + 'd')
else:
past_tense.append(words[i] + 'ed')