如何获取列表中项目重复的计数?

How to get count of item repetition in a list?

您好,我有一个格式如下的列表

tweets= ['RT Find out how AZ is targeting escape pathways to further personalise breastcancer treatment SABCS14', 'Did you know Ontario has a special screening program for women considered high risk for BreastCancer', 'Article Foods That Prevent BreastCancer','PRETTY Infinity Faith Hope Breast Cancer RIBBON SIGN Leather Braided Bracelet breastcancer BreastCancerAwareness']

我刚刚给出了一个列表示例,但它总共有 8183 个元素。所以现在如果我在列表中取第一个项目,我必须将它与列表中的所有其他元素进行比较,如果第一个项目出现在列表中的任何地方,我需要计算它重复了多少次。我尝试了很多可能的方法,但无法达到预期的结果。请帮助,在此先感谢。

我的代码

for x, left in enumerate(tweets1):
   print x,left
   for y, right in enumerate(tweets1):
     print y,right
     common = len(set(left) & set(right))

正如评论中已经指出的那样,您可以使用 collections.Counter 来执行此操作。该代码将翻译成如下内容:

from collections import Counter
tweets = ['RT Find out how AZ is targeting escape pathways to further personalise breastcancer treatment SABCS14',
    'Did you know Ontario has a special screening program for women considered high risk for BreastCancer',
    'Article Foods That Prevent BreastCancer',
    'PRETTY Infinity Faith Hope Breast Cancer RIBBON SIGN Leather Braided Bracelet breastcancer BreastCancerAwareness']

count = Counter(tweets)
for key in Count:
    print key, Count[key]

请注意,Counter 本质上是一个 dict,因此无法保证元素的顺序。