Skip to content Skip to sidebar Skip to footer

Average Number Of Characters Per Word In A List

I'm new to python and i need to calculate the average number of characters per word in a list using these definitions and helper function clean_up. a token is a str that you get f

Solution 1:

Let's clean up some of these functions with imports and generator expressions, shall we?

import string

def clean_up(s):
    # I'm assuming you REQUIRE this function as per your assignment
    # otherwise, just substitute str.strip(string.punctuation) anywhere
    # you'd otherwise call clean_up(str)
    return s.strip(string.punctuation)

def average_word_length(text):
    total_length = sum(len(clean_up(word)) for sentence in text for word in sentence.split())
    num_words = sum(len(sentence.split()) for sentence in text)
    return total_length/num_words

You may notice this actually condenses to a length and unreadable one-liner:

average = sum(len(word.strip(string.punctuation)) for sentence in text for word in sentence.split()) / sum(len(sentence.split()) for sentence in text)

It's gross and disgusting, which is why you shouldn't do it ;). Readability counts and all that.


Solution 2:

This is a short and sweet method to solve your problem that is still readable.

def clean_up(word, punctuation="!\"',;:.-?)([]<>*#\n\\"):
    return word.lower().strip(punctuation)  # you don't really need ".lower()"

def average_word_length(text):
    cleaned_words = [clean_up(w) for w in (w for l in text for w in l.split())]
    return sum(map(len, cleaned_words))/len(cleaned_words)  # Python2 use float

>>> average_word_length(['James Fennimore Cooper\n', 'Peter, Paul and Mary\n'])
5.142857142857143

Burden of all those preconditions falls to you.


Post a Comment for "Average Number Of Characters Per Word In A List"