更多字符串运算符与 Twitter
在本练习中,您将对从 tweets 数据集选取的 3 个字符串应用不同的字符串运算符。变量 tweets_list 已为您创建。
您需要通过应用不同的字符串运算符构造 3 个新列表:
- 仅保留字母的列表
- 仅保留字符的列表
- 仅保留数字的列表
所需函数已从 nltk 为您导入。
本练习是课程的一部分
Python 中的情感分析
练习说明
- 从
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])