【发布时间】:2018-01-16 18:33:45
【问题描述】:
我有一个 Twitter 应用程序,用于梳理有关热门话题的推文。 它创建一个 .txt 文件(称为“单词”),将所有推文中的每个单词解析为列表中的字符串。
目前,要将 twitter 列表中的每个单词与“积极”单词列表进行比较,我有:
def p_count(l): #list of strings is object called upon
total = 0
for w in l: #for each word in twitter 'words' list
for x in p_words: #for each word in "positive" words list
if w == x: #compare twitter word to x positive word
total += 1
return total
print p_count(words)
我得到的结果为 0,但我知道两个列表中都出现了“谦逊”和“坚强”之类的词。我正在使用 Enthought Canopy。有什么建议吗?
【问题讨论】:
-
请提供一个minimal reproducible example,显示
words和p_words的内容。 -
您的方法虽然效率低下,但在逻辑上看起来非常好。也许您需要考虑单词的大小写,以及 API 中单词中的空格。另外,考虑使用集合模块中的 dict 或 Counter 类,它会非常有效。
-
您应该考虑将
p_words作为参数添加到函数中并将其传递给函数
标签: python string twitter counter canopy