You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将生成的过去式单词保存到指定列表past_tense中?

解决方法

你需要先初始化一个空的past_tense列表,然后在遍历过程中把生成的过去式单词添加到这个列表里,而不是只打印出来。修改后的完整代码如下:

words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []  # 初始化空列表用于存储结果

for word in words:
    if word.endswith("e"):  # 用endswith方法判断结尾更简洁直观
        past_form = word + "d"
    else:
        past_form = word + "ed"
    past_tense.append(past_form)  # 将生成的过去式添加到目标列表中

# 可选:打印列表查看结果
print(past_tense)

关键改动说明

  • 新增past_tense = []:创建一个空列表作为存储结果的容器,这是实现存储的前提
  • 替换print为past_tense.append(past_form):append()方法是Python列表添加元素的标准方式,能把生成的过去式单词存入目标列表
  • 用word.endswith("e")替代原判断逻辑:相比通过索引取最后一个字符,endswith()方法更简洁易读,功能完全一致

运行上述代码后,past_tense列表就会包含所有符合要求的过去式单词。

内容的提问来源于stack exchange,提问作者user21481661

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 09:07:20