在python中,将字母追加到列表中每一个字符串的末尾[重复]。

问题描述 投票:0回答:3

我需要把这些词变成过去式,在列表中每个词的结尾处加一个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')
python string list append
3个回答
3
投票

你的治疗 i 如同 words[i]. i 当你做的时候,是字 for i in words:. 替换成 iword. 那么你就不会被索引和单词所迷惑。

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

3
投票
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')

3
投票

当做 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]

2
投票

让自己对列表理解得心应手。

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)

1
投票

这就是你要找的东西,只是需要做一些调整(见评论)。

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

1
投票

当你做 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')

1
投票

这些答案都回答了你的直接问题,但如果你正在寻找一种更通用的方法来确定一个动词的过去时形式(例如,"给 "到 "给了"),一个更细微的库将能够以更高的精确度来处理这个问题。NodeBox 是一个很好的。

python3 -m pip install pattern

那么如解释 此处,你可以使用它的 en 包的动词共轭包。

from pattern.en import conjugate
past_tense = []

for word in words:
    past_tense.append(conjugate(word, tense='past'))
© www.soinside.com 2019 - 2024. All rights reserved.