標籤:int text help gpo 條件判斷 span reading inter pre
python中,可以把多行代碼簡化為一行,把for迴圈和if條件判斷都集中到一行裡來寫,樣本如下:
>>> from nltk.corpus import stopwords>>> english_stopwords = stopwords.words(‘english‘)#載入nltk中的英文停用詞資料
#建立一個列表,內含3個單字清單>>> texts_tokenized = [[‘writing‘, ‘ii‘, ‘rhetorical‘, ‘composing‘, ‘rhetorical‘, ‘composing‘],[‘engages‘, ‘series‘, ‘interactive‘, ‘reading‘],[‘research‘, ‘composing‘, ‘activities‘, ‘along‘, ‘assignments‘, ‘designed‘, ‘help‘]]
#用多行代碼對texts_tokenized去停用詞>>> text_filtered_stopwords = [[word for word in document if not word in english_stopwords] for document in texts_tokenized]>>> text_filtered_stopwords[[‘writing‘, ‘ii‘, ‘rhetorical‘, ‘composing‘, ‘rhetorical‘, ‘composing‘], [‘engages‘, ‘series‘, ‘interactive‘, ‘reading‘], [‘research‘, ‘composing‘, ‘activities‘, ‘along‘, ‘assignments‘, ‘designed‘, ‘help‘]]
然後改成用多行的常規寫法:
>>> texts_tokenized = [[‘writing‘, ‘ii‘, ‘rhetorical‘, ‘composing‘, ‘rhetorical‘, ‘composing‘],[‘engages‘, ‘series‘, ‘interactive‘, ‘reading‘],[‘research‘, ‘composing‘, ‘activities‘, ‘along‘, ‘assignments‘, ‘designed‘, ‘help‘]]>>> documents = []>>> texts_filtered_stopwords =[]>>> for document in texts_tokenized: for word in document: if word not in english_stopwords: documents.append(word) texts_filtered_stopwords.append(document) >>> texts_filtered_stopwords[[‘writing‘, ‘ii‘, ‘rhetorical‘, ‘composing‘, ‘rhetorical‘, ‘composing‘], [‘engages‘, ‘series‘, ‘interactive‘, ‘reading‘], [‘research‘, ‘composing‘, ‘activities‘, ‘along‘, ‘assignments‘, ‘designed‘, ‘help‘]]
可以看到得出一樣的結果,但是代碼的效率和簡潔程度大大提升
python多行代碼簡化