Danny
Danny

Reputation: 85

Counting occurences of words that appear in a list using Python

I have appended excel sheet values to a list using xlrd. I called the list a_master. I have a text file with words that I want to count the occurrences of that appear in this list (I called this file dictionary and theirs 1 word per line). Here is the code:

with open("dictionary.txt","r") as f:
for line in f:
    print "Count " + line + str((a_master).count(line)) 

For some reason though, the count comes back with zero for every count word that exists in the text file. If I write out the count for one of these words myself:

 print str((a_master).count("server"))

It counts the occurrences no problem.I have also tried

print line

in order to see if it is seeing the words in the dictionary.txt file correctly and it is.

Upvotes: 1

Views: 114

Answers (2)

tommy.carstensen
tommy.carstensen

Reputation: 9622

Use collections.Counter():

import re
import collections
words = re.findall(r'\w+', open('dictionary.txt').read().lower())
collections.Counter(words)

Why is this question tagged xlrd by the way?

Upvotes: 0

Abhijit
Abhijit

Reputation: 63787

Lines read from the file is terminated by newline character. There may also be white space at the end. It is better to strip out any whitespace before doing a lookup

with open("dictionary.txt","r") as f:
    for line in f:
        print "Count " + line + str((a_master).count(line.strip())) 

Note Ideally, searching a list is linear and may not be optimal in most cases. I think collections.Counter is suitable for situation as you depicted.

Re-interpret your list as a dictionary where the key is the item and the value is the occurrence by passing it through collections.Counter as shown below

a_master = collections.Counter(a_master)

and you can re-write your code as

from itertools import imap
with open("dictionary.txt","r") as f:
    for line in imap(str.strip, f):
        print "Count {} {}".format(line, a_master[line])

Upvotes: 1

Related Questions