I have a question about my homework. Here's the problem: Write a program that reads a text file called input.txt, which contains an arbitrary number of lines in the form of ",", then writes this information using a dictionary and finally displays a list of countries represented in the file, and the number cities.
For example, if input.txt contains the following:
New York, USA
Angers, France
Los Angeles, USA
Pau, France
Dunkirk, France
Mecca, Saudi Arabia
The program displays the following (in some order):
Saudi Arabia: 1
USA: 2
France: 3
Here is my code:
def addword(w,wcDict): if w in wcDict: wcDict[w] +=1 else: wcDict[w]= 1 import string def processLine(line, wcDict): wordlist= line.strip().split(",") for word in wordlist: word= word.lower().strip() word=word.strip(string.punctuation) addword(wordlist[1], wcDict) def prettyprint(wcDict): valkeylist= [(val,key) for key,val in wcDict.items()] valkeylist.sort(reverse = True) for val,key in valkeylist: print '%-12s %3d'%(key,val) def main(): wcDict={} fobj= open('prob1.txt','r') for line in fobj: processLine(line, wcDict) prettyprint (wcDict) main()
My code counts each country twice. Can you help me?
thanks
source share