我已经从NLTK库下载了路透社语料库,并希望在一个新变量中存储10个随机文档,其中包含50多个元素。
我已经下载了该语料库并编写了以下代码,但它不间断地连续运行:
import nltk
nltk.download('reuters')
nltk.download('punkt')
from nltk.corpus import reuters
sample_data = []
for i in range(len(reuters.sents())):
sent = random.choice(reuters.sents())
if len(sent) <= 50: # Skips the sentence if it contains less than 50 elements
pass
else:
sample_data.append(sent)
while len(sample_data) == 10:
break是否有一种更有效的方式来编写这个程序来完成我的命令?
发布于 2022-02-15 04:20:53
尝试使用if而不是while:
if len(sample_data) == 10:
breakhttps://stackoverflow.com/questions/70013064
复制相似问题