更多字串運算子與 Twitter
在這個練習中,你會對從 tweets 資料集挑選出的 3 個字串套用不同的字串運算子。已為你建立 tweets_list。
你需要透過不同的字串運算子,建立 3 個新的清單:
- 只保留英文字母的清單
- 只保留字元的清單
- 只保留數字的清單
所需的函式已從 nltk 匯入。
本練習屬於課程
Python 情感分析
練習說明
- 從
tweets_list建立詞元的清單。 - 在清單
letters中移除所有數字與其他符號,也就是只保留英文字母。 - 在
let_digits中保留英數字元(alphanumeric),移除其他所有符號。 - 建立
digits:移除英文字母與其他符號,只保留數字。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a list of lists, containing the tokens from list_tweets
tokens = [____(item) for item in tweets_list]
# Remove characters and digits , i.e. retain only letters
letters = [[word for word in item if ____.____] for item in tokens]
# Remove characters, i.e. retain only letters and digits
let_digits = [[word for word in item if ____.____] for item in tokens]
# Remove letters and characters, retain only digits
digits = [[word for word in item if ____.____] for item in tokens]
# Print the last item in each list
print('Last item in alphabetic list: ', letters[2])
print('Last item in list of alphanumerics: ', let_digits[2])
print('Last item in the list of digits: ', digits[2])