0

Hi i have a list in the following format

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']

I have just given a sample of list but it has a total of 8183 elemets. So now if i take 1st item in the list i have to compare that with all the other elements in the list and if 1st item appears anywhere in the list i need the count how many times it got repeated. I tried many ways possible but couldnt achieve desired result. Please help, thanks in advance.

my code

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

1 Answers1

1

As already pointed out in comments, you can use collections.Counter to do this. The code will translate into something like below:

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]

Note that the Counter is essentially a dict, and so the order of the elements will not be guaranteed.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186