为什么我的python集合计数器返回1?

2024-04-24 06:31:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个单词的列表,需要每个单词的总数。你知道吗

两条线:

['ashtonsos', 'i', 'heard', 'you', 'shouldnt', 'trust', 'claires', 'with', 'piercings', 'lol']
['liveaidstyles', 'thank', 'you', 'so', 'much', '\xf0\x9f\x92\x98']

我已经导入了collections计数器,使用“from collections import counter”行

这是我的密码:

         for word in words:
            if word not in unique_words:
                unique_words.append(word)   
        #print unique_words

            tweet_count = Counter(unique_words) 
            for word in unique_words:
                tweet_count.update()


    for word in tweet_count:
        print word, tweet_count[word]

打印的是每个单词后面跟一个1,即使这个单词是重复的。所以,基本上,计数器不计数。你知道吗

仅供参考,“.update()”行…我还使用了“tweet\u count+=1”。。。它返回相同的结果。你知道吗

我做错什么了??你知道吗


Tags: inyou列表forcount计数器update单词
2条回答

不是很明显吗?你在数一列unique_words。唯一的,根据定义,只发生一次。你知道吗

试试这个:

counter = Counter()
for my_list in my_list_of_lists:
    counter += Counter(set(my_list))

修改为:

        for word in words:
            if word not in AFINN and word not in unique_words:
                unique_words.append(word) 

        for word in unique_words:
            tweet_count[word] = tweet_count.get(word,0) + 1 

相关问题 更多 >