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?
total = 0 with open('input.txt') as f: for line in f: finded = line.find('California') if finded != -1 and finded != 0: total += 1 print total