patternpythonModerate
Count the frequency of words in a text file
Viewed 0 times
thefilefrequencywordstextcount
Problem
#!/usr/bin/python
file=open("C:/python27/python operators.txt","r+")
wordcount={}
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v in wordcount.items():
print k, vSolution
Use a Context Manager
When dealing with something like a file, it's safer to write:
That will automatically handle file closing correctly in case of exceptions and the like. I also find it clearer. Also, why
Prefer Generators
Use the tools at your disposal
You have:
That's a lot of code for a simple operation. What are you doing here? You're counting the incidence of
Furthermore, we also have
Full solution:
When dealing with something like a file, it's safer to write:
with open(filename) as file:
## code ##That will automatically handle file closing correctly in case of exceptions and the like. I also find it clearer. Also, why
r+?Prefer Generators
file.read() reads the entire file into memory. Prefer to just go a line at a time by iterating through it. wordcount.items() gives you a full list of all the items. But you don't need them all at once, you just need to iterate through them. For that there's iteritems(). Use the tools at your disposal
You have:
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1That's a lot of code for a simple operation. What are you doing here? You're counting the incidence of
word. But there's an app for that: collections.Counter:wordcount = collections.Counter()
for line in file:
for word in line.split():
wordcount[word] += 1Furthermore, we also have
Counter.update, which lets us do:for line in file:
wordcount.update(line.split())Full solution:
#!/usr/bin/python
import collections
wordcount = collections.Counter()
with open("C:/python27/python operators.txt") as file:
for line in file:
wordcount.update(line.split())
for k,v in wordcount.iteritems():
print k, vCode Snippets
with open(filename) as file:
## code ##if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1wordcount = collections.Counter()
for line in file:
for word in line.split():
wordcount[word] += 1for line in file:
wordcount.update(line.split())#!/usr/bin/python
import collections
wordcount = collections.Counter()
with open("C:/python27/python operators.txt") as file:
for line in file:
wordcount.update(line.split())
for k,v in wordcount.iteritems():
print k, vContext
StackExchange Code Review Q#103773, answer score: 12
Revisions (0)
No revisions yet.