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)

Edited 5 Years Ago by joeywheels: n/a

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

Edited 5 Years Ago by woooee: n/a

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

Edited 5 Years Ago by richieking: n/a

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...

This question has already been answered. Start a new discussion instead.