问题描述
我想知道我是否可以让 Sentimentia.polarity_scores() 函数只打印负数,只打印正数,我可以摆脱中性和复合结果
i = 0
while i < len(Replaced_Data):
Sentimentia = SentimentIntensityAnalyzer()
print(Sentimentia.polarity_scores(data['Clean_TweetText'][i]))
i = i + 1
output >>> {'neg': 0.214,'neu': 0.534,'pos': 0.252,'compound': 0.25}
desired output >>> {'neg': 0.214,'pos': 0.188}
解决方法
可以使用 Dictionary Comprehensions
从字典中过滤 'neg' 和 'pos'ss = Sentimentia.polarity_scores(data['Clean_TweetText'][i])
print({e:ss[e] for e in ss if e in ['neg','pos']})