求写个python代码

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


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}')

单词表和语料在哪?

这里用几个单词和例句作为例子,具体的参数可以自己改

import random

if __name__ == '__main__':
    # 单词列表
    en_word = ['terrible', 'examine', 'conscience', 'shut', 'unbearable']
    # 例句列表
    example = [
        'He lost both his legs in a terrible car accident',
        'The doctor examined the son of the mayor carefully',
        'She was afflicted with conscience',
        'He shut his eyes to her faults.',
        'The annoying noise is unbearable to him'
    ]
    # 随机取2个单词
    select_word = random.sample(en_word, 2)
    # 循环选取的单词,去例句中找
    for word in select_word:
        # 找到包含该单词的例句
        select_example = list(filter(lambda x: word in x, example))
        # 如果找到的多于10个
        if len(select_example) > 10:
            # 就只取前十个
            select_example = select_example.fetch(10)
        print("{}单词的例句有:{}".format(word, select_example))
  1. 代码
import random

words = []
with open('word.txt') as fs:
    words = [i.strip() for i in fs.readlines()]
lines = []
with open('line.txt') as fs:
    lines = [i.strip() for i in fs.readlines()]

result = {}
for word in random.sample(words, 10):
    result[word] = []
    for line in lines:
        if word in line:
            if len(result[word]) == 10:
                break
            else:
                result[word].append(line)
print(result)
  1. 单词文本
Installing
Naming
your
a
d
g
r
a
g
a
d
f
  1. 句子文本
Not having the Pillow package installed by running pip install Pillow.
Installing the package in a different Python version than the one you're using.
Installing the package globally and not in your virtual environment.
Your IDE running an incorrect version of Python.
Naming your module pil.py which would shadow the official module.
Declaring a variable named PIL which would shadow the imported variable.
Not having the Pillow package installed by running pip install Pillow.
Installing the package in a different Python version than the one you're using.
Installing the package globally and not in your virtual environment.
Your IDE running an incorrect version of Python.
Naming your module pil.py which would shadow the official module.
Declaring a variable named PIL which would shadow the imported variable.
Not having the Pillow package installed by running pip install Pillow.
Installing the package in a different Python version than the one you're using.
Installing the package globally and not in your virtual environment.
Your IDE running an incorrect version of Python.
Naming your module pil.py which would shadow the official module.
Declaring a variable named PIL which would shadow the imported variable.