m0_73701533 2022-11-05 10:24 采纳率: 100%
浏览 144
已结题

求写个python代码

题目:从综合英语单词表里,挑选10个单词,从语料里为每个单词找至少一个例句,如果例句过多,则只保留10个例句
怎么写代码

  • 写回答

4条回答 默认 最新

  • honestman_ 2022-11-05 14:25
    关注
    
    import random
    
    
    words = ['hello', 'word', 'python']
    # 语料库, 可以选择从文件读取,
    text = ['hello, how are you',
            'this word is bad',
            'python is the best'
            ]
    # 挑选两个(10)单词
    words = [random.choice(words) for _ in range(2)]
    # 寻找例句
    for word in words:
        text_list = []
        for t in text:
            if word in t:
                text_list.append(t)
                if len(text_list)>=10:  # 最多10个例句
                    break
        print(f'{word}的例句有:{text_list}')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月6日
  • 已采纳回答 11月6日
  • 创建了问题 11月5日