Python计算UNIQUE出现次数...不是总计数。怎么样?

时间:2014-07-14 23:15:46

标签: python count unique counter tweets

更新:以下代码似乎可以解决问题:

        for word in words:
            if word not in unique_words: ##skips replicates in a given list
                unique_words.append(word)   

在我的生活中,我无法弄清楚如何计算找到单词的列表数量,而不是列表中找到单词的次数。

如果我设置了一个计数器...它会计算单词的出现次数。但是,我需要的东西,因为它遍历单词列表(每个列表是推文),计算一个单词的独特出现次数(例如,有这个单词的推文的数量)。计数器,因为我知道如何使用它们,与集合计数器相同...计算所有实例。

FYI ......循环迭代的是推文,每个推文都应用了.split()函数......所以,每个推文都是一个单词列表。

这是我的代码,它没有做我需要做的事情。

        sentiment = 0   

        for word in words:
            if word in AFINN:
                sentiment += AFINN[word]                

        for word in words:
            if word not in AFINN and word not in new_sent:
                new_sent[word] = sentiment
                tweet_count[word] = 1

            elif word in new_sent:
                new_sent[word] = new_sent[word] + sentiment
                tweet_count[word] += 1

另外......我在单词陈述中有两个单词,因为这是我能够获得TOTAL情绪分数的唯一方式,而不仅仅是找到新单词的分数。不过,我想,如果我回来了。情绪,我可能不需要双循环?但是,就目前而言,这不是我最关心的问题。

感谢!!!

3 个答案:

答案 0 :(得分:1)

因此,如果我理解正确,您会收到一堆推文,并且您想查看这些推文中某个字出现的次数?

count = 0
for tweet in tweets:
    if word in tweet:
        count += 1

当然,如果你真的不想详细说明......

sum([word in tweet for tweet in tweets])

protip:尝试求和([真,真,真,假,真])

如果我误解了某些事情,请告诉我。

答案 1 :(得分:0)

怎么样:

tweets_with_word = defaultdict(int)

for words in tweets:  # `tweets` is the "outside" list of word lists
    for word in set(words):
        tweets_with_word[word] += 1

    for word in words:
        ...  # The rest of your code

基本上,使用set()获取每条推文中的唯一字词列表,然后只计算这些实例。

答案 2 :(得分:0)

在你写的更新中(在帖子的最后部分):

for word in words: if word not in unique_words: ##skips replicates in a given list unique_words.append(word)

现在我很困惑。如果这正是您想要的,那么为什么不使用collections.OrderedDict

from collections import OrderedDict words = ['good', 'good', 'bad', 'terrible', 'lucky'] unique_words = OrderedDict.fromkeys(words)

不需要所有这些控制结构。

如果单词的顺序无关紧要,那么只需使用set

unique_words = set(words)