我如何使用 python 3.x 在一行中编写这些类型的循环
How do i write these type of loops in one line using python 3.x
我是 python 的新手。我正在尝试为我的 NLP 项目清理一个文本文件。怎么把这几行代码写成一行python。我正在使用 NLTK 来处理我的文本数据。
sentences = [sent_tokenize(token) for token in doc]
words = [word_tokenize(''.join(sentence)) for sentence in sentences]
clean_words=[]
for word in words :
for token in word :
if token.isalnum() and token.lower() not in list(stop_words):
clean_words.append(token.lower())`
我猜你指的是嵌套列表理解?像这样:
clean_words = [t.lower() for w in words for t in w if t.isalnum() and t.lower() not in list(stop_words)]
我是 python 的新手。我正在尝试为我的 NLP 项目清理一个文本文件。怎么把这几行代码写成一行python。我正在使用 NLTK 来处理我的文本数据。
sentences = [sent_tokenize(token) for token in doc]
words = [word_tokenize(''.join(sentence)) for sentence in sentences]
clean_words=[]
for word in words :
for token in word :
if token.isalnum() and token.lower() not in list(stop_words):
clean_words.append(token.lower())`
我猜你指的是嵌套列表理解?像这样:
clean_words = [t.lower() for w in words for t in w if t.isalnum() and t.lower() not in list(stop_words)]