Python - Return Average of Word Length In Sentences
<p>I have a question very similar to this one: <a href="http://stackoverflow.com/questions/12761510/python-how-can-i-calculate-the-average-word-length-in-a-sentence-using-the-spl">Python: How can I calculate the average word length in a sentence using the .split command?</a></p>

<p>I need the average word length for multiple sentences. Here's what I currently have. I'm getting the average of all the words when I want it by sentence. Also getting a 0 at the end of the first row that's produced.</p>

<pre><code>words = "This is great. Just great."
words = words.split('.')
words = [sentence.split() for sentence in words]
words2 = [len(sentence) for sentence in words]
average = sum(len(word) for word in words)/len(words)
print(words2)
print(average)
</code></pre>

<p>Try this:</p>

<pre><code>data = "This is great. Just great."
sentences = data.split('.')[:-1]    #you need to account for the empty string as a result of the split
num_words = [len(sentence.split(' ')) for sentence in sentences]
average = sum(num for num in num_words)/(len(sentences))
print(num_words)
print(len(sentences))
print(average)
</code></pre>

python string