954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Average Word Length

This is a modified program from the word count program that I posted about. I am trying to calculate the average word length in a sentence. Here is my code so far:

def main():
    print "This program will calculate the average word length in a sentence"
    print
    s = raw_input("Enter a sentence: ")
    words = string.split(s)
    wordCount = len(words)
    ch = string.split(words)
    charCount = len(ch)
    avgLength = charCount / wordCount
    print "The average word length of the sentence '", s,"', is", avgLength,"words."
main()

I keep getting an error like 'list' object has no attribute 'split'.

pyguy25
Newbie Poster
10 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

I am a little confused with the 'string.split(s)' I thought it was more like s.split():

s = "Just a test string"
words = s.split()
print words


Are there different versions of Python floating about?

Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
 

Well, yes ... but they are strictly upgrades, not forks. The 'string.split(s)' is a now-deprecated way of writing 's.split()'

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 
I keep getting an error like 'list' object has no attribute 'split'.


And you really don't know why?

words = string.split(s)

Ok, from your other post you know thatwords is a list. Fine.

ch = string.split(words)

What are you doing here? You want to split a list? Worse than that, you want to do it withstring.split()! ;)

Here is a receipe how to find the average word-length:
1. Split the string into words (hint: We have that already ;))
2. Do something like total = 0 . We need this later
3. Iterate over the words in the list. (hint: for word in words: )
4. Find the length of the current word (hint: a word is just alist of characters ;))
5. Add this length to total.

6. Ok, after we've done this with every word, total is the sum of the wordlengths.
7. Divide total by the number of words.
8. That's it.

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

Okay, I'll try that. Sorry, I'm a beginner at Python so I might ask stupid or obvious questions. Just bare with me. It'll come easier to me eventually.

pyguy25
Newbie Poster
10 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

ok, got it:

def main():
    print "This program will calculate the average word length in a sentence"
    print
    s = raw_input("Enter a sentence: ")
    words = string.split(s)
    wordCount = len(words)
    sum = 0
    for word in words:
        ch = len(word)
        sum = sum + ch
    avg = sum / wordCount
    if avg is 1:
        print "In the sentence '", s,"', the average word length is", avg,"letter."
    else:
        print "In the sentence '", s,"', the average word length is", avg,"letters."
main()
pyguy25
Newbie Poster
10 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 
Okay, I'll try that. Sorry, I'm a beginner at Python so I might ask stupid or obvious questions. Just bare with me. It'll come easier to me eventually.


If my posts sound somewhat rude, I'm sorry for that. It's not meant this way.
Your questions are absolutely not stupid, and before you ask you try to find a solution by yourself. That's more than some others here do.

Ok, just one comment on your code:

sum = 0

There is a builtin-function calledsum(). It's bad style to call variables like builtins, and can lead to very confusing errors.

Here's a shorter version of your code(using sum):

s = "This is a short sentence"
l = s.split()
print sum( [ len(word) for word in l ] ) / len(l)


Regards, mawe

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

Good job! Just to make your code robust, you should check for dividing by zero:

if wordCount != 0:
    avg = sum / wordCount
else:
    avg = 0


If you want to continue to improve the code beyond that, you might consider turning the average word count into a separate function, so that it could be used by other programs for any old purpose. Doing so would have the positive side effect of cleaning up your code.

In general, it's usually better to have lots of small functions that do one thing rather than a few number of large functions that do lots of things. Your main() interacts with the user, finds the average, and prints the results ... a bit too Swiss-army-knifish for my taste. :)

Also, you might consider reading the Python docs tutorial on list generators. A list generator can simplfiy your code like this:

total = sum([len(word) for word in words]


That one line replaces the entire sum=0 and for-loop section.

Jeff

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

split will break if your input have punctuations. eg word.Test . This will be counted as 1?
(although a blank space comes after a full stop.)
anyway, here's another way to do it,

>>> import re
>>> s = "This program will calculate the average word length in a sentence.Tehas dfsdskj 323 5543"
>>> len(re.findall("\w+",s))
15
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You