user3072782
user3072782

Reputation: 49

comparing occurrence of strings in list in python

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?

Upvotes: 1

Views: 172

Answers (2)

Roberto
Roberto

Reputation: 2786

I had posted an answer, but I see it's for an assignment, so I'll try to explain instead of just splatting a solution here.

My suggestion would be to solve it in three steps:

1) in the first line, create a list of sorted characters that appear in the string:

  • from the data string you can use set(data) to pick every unique character
  • if you use sort() on this set you can create a list of characters, sorted alphabetically.

2) then use this list in a for loop (or list comprehension) to create a second list, of their number of occurrences in data, using data.count(<letter in the list>); note that the elements in this second list are technically sorted based on the alphabetical order of the letters in the first list you made (because of the for loop).

3) compare this second list of values with a sorted version of itself (now sorted by values), and see if they match or not. If they don't match, it's because some of the initial letters appears too many times compared to the next ones.

Upvotes: 1

dstromberg
dstromberg

Reputation: 7167

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_).

Upvotes: 1

Related Questions