我正在尝试编写一个函数 prevword_ave_len(word),它接受一个字符串arugment word 并返回之前的单词的平均字符长度
中的文字该文是Moby Dick的第一段:
叫我以实玛利。几年前 - 没关系多长时间 - 我的钱包里没有钱,也没有什么特别令我感兴趣的,我觉得我会稍微航行一下,看看这个世界的水域。这是我驱除脾脏和调节血液循环的一种方式。每当我发现自己的嘴巴变得严峻时;无论什么时候,我的灵魂都是一个潮湿,细雨的十一月;每当我发现自己在棺材仓库前不由自主地停顿,并抬起我遇到的每一次葬礼的后方;特别是每当我的hypos得到我这样的优势时,它需要一个强烈的道德原则来阻止我故意走进街道,并有条不紊地敲掉人们的帽子 - 然后,我认为现在是时候尽快出海了如我所能。这是我用手枪和球的替代品。随着哲学的蓬勃发展,卡托将自己扔在剑上;我悄悄带上了船。这没什么好奇怪的。如果他们知道这一点,那么几乎所有学位的人,无论是时间还是其他人,都会像我一样珍惜与海洋相同的感情。
有一些特殊要求需要注意:
我该怎么做呢?我的思考过程是将文本拆分为单词列表,然后使用for循环搜索单词的每个实例,找到单词的位置,以某种方式在 word <之前对单词进行索引/ strong>,找到它的长度并将其添加到空列表中。然后我会对此列表中的元素进行平均,然后这将是我的输出。我只是不知道该怎么做呢?
答案 0 :(得分:0)
此解决方案使用字典,其中的值是所有前面单词的长度列表。
给出的示例打印单词the
(最后一行)的解决方案。
如果您不熟悉defaultdict
,请查看here。
from collections import defaultdict
def prevword_ave_len(word, text):
words = defaultdict(list) #initialization of default dictionary
textlist = text.split() #split text into words
words[textlist[0]].append(0) #append 0 for first word in text
#iterate over words, append length of preceding word to values
for i in range(1,len(textlist)):
words[textlist[i]].append(len(textlist[i-1]))
if word in words:
return sum(words[word])/len(words[word]) #calculate mean
else: return False
if __name__ == "__main__":
text = "Call me Ishmael. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off - then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me."
print(prevword_ave_len('the', text))