def average_word_length(text): ''' Return the average length of all words in text. Do not include surrounding punctuation in words. text is a non-empty list of strings each ending in \n. At least one line in text contains a word.''' words = text.split() for word in words: average=sum(float(len(word)) for word in words)/float(len(words)) return average
I would approach it by first removing the '\n' from the string and removing the punctuation from the string.After that, split the string by looking for the spaces in it, which should provide you with a tuple of each word in the string.Then, loop each word getting its length and calculate an average from each word.Here is a guess at the code below. I haven't tested this so who knows if the syntax is correct...Also look here: http://stackoverflow.com/questions/12761510/python-how-can-i-calculate-the-average-word-length-in-a-sentence-using-the-splBest of luck! text = ['James Fennimore Cooper\n', 'Peter, Paul and Mary\n'] for scen in text: string = scen.replace('\n','') #this would be any punctuation you want to replace string = string.replace("'","") #confirm syntax string = string.split(' ') wordcount = len(string) wordlen = 0 for word in string: tmplen = 0 for letter in word: tmplen += 1 wordlen += tmplen avglen = (wordcount/wordlen) print avglen
text = ['James Fennimore Cooper\n', 'Peter, Paul and Mary\n'] for scen in text: string = scen.replace('\n','') #this would be any punctuation you want to replace string = string.replace("'","") #confirm syntax string = string.split(' ') wordcount = len(string) wordlen = 0 for word in string: tmplen = 0 for letter in word: tmplen += 1 wordlen += tmplen avglen = (wordcount/wordlen) print avglen
def average_word_length(text): """ (list of str) -> float Precondition: text is non-empty. Each str in text ends with \n and at least one str in text contains more than just \n. Return the average length of all words in text. Surrounding punctuation is not counted as part of the words. >>> text = ['James Fennimore Cooper\n', 'Peter, Paul and Mary\n'] >>> average_word_length(text) 5.142857142857143 """
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.