comparing occurrence of strings in list in python
Question: i'm super duper new in python. I'm kinda stuck for one of my class exercises.
The question goes something like this: You have a file that contains
characters i.e. words. (I'm still at the stage where all the terms get mixed
up, I apologize if that is not the correct term) Example of the file.txt
content: accbd

The question asks me to import the file to python editor and make sure that no
letter occurs more than letter that comes later than it in the alphabet. e.g.
a cannot occur more frequently than b; b cannot occur more than c, and so on.
In the example file, c occurs more frequently than d, so I need to raise an
error message.

Here's my pathetic attempt :

    
    
    def main():
    
    f=open('.txt','r') # 1st import the file and open it. 
    
    data = f.read() #2nd read the file
    
    words = list(data) #3rd create a list that contains every letter
    
    newwords = sorted(words) # sort according to alphabetical order
    

I'm stuck at the last part which is to count that the former word doesn't
occur more than the later word, and so on. I tried two ways but neither is
working. Here's trial 1: from collections import counter

    
    
    for i in newwords:
        try:
            if counter(i) <=counter(i+1):
                print 'ok'
            else:
                print 'not ok between indexes %d and %d' % (i, i+1)
         except:
            pass
    

The 2nd trial is similar

    
    
    for i in newwords:
        try:
            if newwords.count(i) <= newwords.count(i+1):
                print 'ok'
            else:
                print 'ok between indexes %d and %d' % (i, i+1)
        except:
            pass
    

What is the correct way to compare the count for each word in sequential
order?


Answer: To be a little more clear:

    
    
    In [2]: string = 'accbd'
    
    In [3]: import collections
    
    In [4]: collections.Counter(string)
    Out[4]: Counter({'c': 2, 'a': 1, 'b': 1, 'd': 1})
    

Then it's just a for loop with enumerate(list_).

