In Python, you can simplify multiple lines of code into one line, with the For loop and if condition judgments all in one line, as shown in the following example:
>>> fromNltk.corpusImportStopwords>>> english_stopwords = Stopwords.words ('中文版') #加载nltk中的英文停用词数据
#创建一个列表 with a list of 3 words>>> texts_tokenized = [['Writing','II','Rhetorical','Composing','Rhetorical','Composing'],['engages','Series','Interactive','Reading'],[' the','Composing','Activities','along','Assignments','designed',' Help']]
#用多行代码对texts_tokenized去停用词>>> text_filtered_stopwords = [[[Word forWordinchDocumentif notWordinchEnglish_stopwords] forDocumentinchTexts_tokenized]>>>text_filtered_stopwords[['Writing','II','Rhetorical','Composing','Rhetorical','Composing'], ['engages','Series','Interactive','Reading'], [' the','Composing','Activities','along','Assignments','designed',' Help']]
Then change to the general wording using multiple lines:
>>> texts_tokenized = [['Writing','II','Rhetorical','Composing','Rhetorical','Composing'],['engages','Series','Interactive','Reading'],[' the','Composing','Activities','along','Assignments','designed',' Help']]>>> documents = []>>> texts_filtered_stopwords =[]>>> forDocumentinchtexts_tokenized: forWordinchDocument:ifWord not inchenglish_stopwords:documents.append (Word) texts_filtered_stopwords.append (document)>>>texts_filtered_stopwords[['Writing','II','Rhetorical','Composing','Rhetorical','Composing'], ['engages','Series','Interactive','Reading'], [' the','Composing','Activities','along','Assignments','designed',' Help']]
You can see the same results, but the code is much more efficient and concise.
Python multi-line code simplification