I'm trying to count the length of each word in a string of text and then count the frequency of times that number of characters shows up throughout the entire text. I can't seem to figure out where to go from what I have here. Any help is greatly appreciated.

import sys

text = "This is an example of a long text of which I am trying to count word lengths and the frequency of each length."
word_list = []

text = text.join().strip()

for punc in "!',?;.":
	text = text.replace(punc, "")

while True:
	words = text.lower().split()
	for word in words:
		word_count = len(word)
		word_list.append(word_count)

Recommended Answers

All 6 Replies

You never exit the "while True" so it is an infinite loop. Once you eliminate that, you should be able to print the final word_list list which will be a series of numbers. Sort the list and count each of the sequences of the same number. You should also get an error on this statement
text = text.join().strip()
What are you trying to do? Take a look at this doc to see what join() does http://docs.python.org/library/stdtypes.html

Thanks woooee. I'll try it out.

can you tell me why you inported sys module???
also break help here...

while True:
	words = text.lower().split()
	for word in words:
		word_count = len(word)
		word_list.append(word_count)
        break;
print word_count,word_list

"while True" serves no purpose in this program.

The while loop was the culprit. I took it out and the program is achieving the desired results at this point. woooee I'm actually calling a text file and then joining the file to my variable 'text'. I forgot to take it out on this example. Thanks for catching that too.

Much thanks to woooee and richieking. Now on to sorting the list and counting each of the sequences of the same number...

well mark thread as solved ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.