File Processing Problem

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

+4
source share
2 answers

In the processLine function, you have an extraneous loop. wordlist will always contain two entries, a city and a country. This way, the code inside the for loop (including addword ) will execute twice - you can just completely remove the for statement, and it should work as you expect.

+2
source
 from collections import Counter as c lines = (line.strip() for line in open("file.txt")) data = (elem for elem in lines) result = [two for one in data for two in one.split(",")] c = Counter() c(result) 

I hope I answered your request

0
source

Source: https://habr.com/ru/post/1348024/


All Articles