题目:从综合英语单词表里,挑选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}')
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报