さらに学ぶ文字列演算子とTwitter
この演習では、tweets データセットから選んだ3つの文字列に、さまざまな文字列演算子を適用します。tweets_list はあらかじめ用意されています。
次の文字列演算子を適用して、3つの新しいリストを作成してください。
- 文字(アルファベット)のみを保持するリスト
- 英数字のみを保持するリスト
- 数字のみを保持するリスト
必要な関数は nltk からインポート済みです。
この演習はコースの一部です
Pythonで学ぶSentiment Analysis
演習の手順
tweets_listからトークンのリストを作成します。- リスト
lettersでは、数字とその他の記号をすべて除き、文字(アルファベット)のみを残します。 let_digitsでは英数字を保持し、それ以外の記号はすべて削除します。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])