| | |
Average Word Count in a text
Thread Solved |
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 1
Hi,
I am very new to Python and computer programming language. I have been working on a text file where I want to find the average length of words in a text file. Well, to start with:
Let's say I have only one sentence in my text file (we can worry about the multiple sentences later). Here's the text:
"But Buffet wrote that he remains hopeful about the long-term prospects for his company and the nation despite the turmoil shaking the world's economies."
I can find the length of each word in this sentence by using the following code;
>>> myfile = open("c:/test/oneline.txt","r")
>>> for line in myfile:
... words=line.split()
... wordcounts = len(words)
... for word in words:
... lengthword = len(word)
... print lengthword
which gives me
3
6
5
4
2
7
7
....and so on.
My problem is writing the rest of the code i.e. summing this up and dividing it by the total number of words.
Any help?
Thanks in advance.
I am very new to Python and computer programming language. I have been working on a text file where I want to find the average length of words in a text file. Well, to start with:
Let's say I have only one sentence in my text file (we can worry about the multiple sentences later). Here's the text:
"But Buffet wrote that he remains hopeful about the long-term prospects for his company and the nation despite the turmoil shaking the world's economies."
I can find the length of each word in this sentence by using the following code;
>>> myfile = open("c:/test/oneline.txt","r")
>>> for line in myfile:
... words=line.split()
... wordcounts = len(words)
... for word in words:
... lengthword = len(word)
... print lengthword
which gives me
3
6
5
4
2
7
7
....and so on.
My problem is writing the rest of the code i.e. summing this up and dividing it by the total number of words.
Any help?
Thanks in advance.
•
•
Join Date: Jun 2008
Posts: 122
Reputation:
Solved Threads: 30
First of all, please read this. It is on the very beginning of the forum. Your indent cannot be reconstructed from your post, so your code can only be guessed.
I think your problem is not, that you are new to python. In any language there would be a problem with that solution.
How would you say your algo in real words?
You want to divide the sum of wordlentghts with the sum of word pieces.
Your code does not sum up the pieces and the lenghts, just stores and prints the last length.
I think your problem is not, that you are new to python. In any language there would be a problem with that solution.
How would you say your algo in real words?
You want to divide the sum of wordlentghts with the sum of word pieces.
Python Syntax (Toggle Plain Text)
sum_of_wordlengths=0 sum_of_wordpieces=0 For all line in the file: sum_of_wordlengths=sum_of_wordlengths+ length of words in the line sum_of_wordpieces=sum_of_wordpieces+ number of words in the line print sum_of_wordlengths/sum_of_wordpieces
Your code does not sum up the pieces and the lenghts, just stores and prints the last length.
Last edited by slate; May 29th, 2009 at 10:08 pm.
ararik, please do not double post!
You are almost there, as slate says, simply sum up word counts and word lenghts:
You are almost there, as slate says, simply sum up word counts and word lenghts:
python Syntax (Toggle Plain Text)
myfile = open(filename, "r") wordcount_sum = 0 wordlength_sum = 0 for line in myfile: words = line.split() # sum up the word counts wordcount_sum += len(words) for word in words: # sum up the word lengths wordlength_sum += len(word) # invoke floating point division for Python versions < 3.0 wordlength_average = wordlength_sum/float(wordcount_sum)
Last edited by sneekula; May 30th, 2009 at 11:22 am.
No one died when Clinton lied.
![]() |
Similar Threads
- word count problem (C)
- Average Word Length (Python)
- Word count help. (C++)
- Can you please help me write this word count program in another way (Python)
- word count in borland c++ ?? (C++)
- word count (Java)
- I can't implement a word count into my text editor (JAVA) (Java)
Other Threads in the Python Forum
- Previous Thread: wxPython - how do I set a windows on the top of others??
- Next Thread: Simple File Operation Question
| Thread Tools | Search this Thread |
address aliased anydbm bash beginner bits calling casino changecolor class clear conversion convert corners count cturtle cursor curves definedlines dictionary digital dynamic dynamically events examples excel external file float format frange function gui handling hints homework i/o iframe import info input java line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton string strings subprocess table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode urllib urllib2 valueerror variable web-scrape whileloop word wxpython






