Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Count how many times a word occurs in a file

Tags:

python

file

count

I have a file that contains a city name and then a state name per line in the file. I am suppose to count how many times a state name occurs and return the value.

for example, if my file contained:

Los Angeles   California
San Diego     California
San Francisco California
Albany        New York
Buffalo       New York
Orlando       Florida

I am suppose to return how many times each state name occurs. I have this for California.

for line in f:
    California_count=line.find("California")
    if California_count!=-1:
        total=line.count("California")
print(total)

This only gives me the value 1, which I am assuming is because it occurs 1 time per line. How do I get it to return the number 3 instead of the number 1?

like image 980
bw61293 Avatar asked Dec 04 '22 07:12

bw61293


1 Answers

Use dictionary for storing counters:

data = """Los Angeles   California
San Diego     California
San Francisco California
Albany        New York
Buffalo       New York
Orlando       Florida""".splitlines()

counters = {}
for line in data:
    city, state = line[:14], line[14:]
    # city, state = line.split('\t') # if separated by tabulator
    if state not in counters:
        counters[state] = 1
    else:
        counters[state] += 1
print counters
# {'Florida': 1, 'New York': 2, 'California': 3}

You can simplify it by using collections.defaultdict:

from collections import defaultdict
counter = defaultdict(int)
for line in data:
    city, state = line[:14], line[14:]
    counter[state] += 1

print counter
# defaultdict(<type 'int'>, {'Florida': 1, 'New York': 2, 'California': 3})

or using collections.Counter and generator expression:

from collections import Counter
states = Counter(line[14:] for line in data)
# Counter({'California': 3, 'New York': 2, 'Florida': 1})
like image 129
m.wasowski Avatar answered Dec 08 '22 06:12

m.wasowski