Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 查找文本字符串中另一个单词的前一个单词的平均长度_Function_Loops_Python 3.x - Fatal编程技术网

Function 查找文本字符串中另一个单词的前一个单词的平均长度

Function 查找文本字符串中另一个单词的前一个单词的平均长度,function,loops,python-3.x,Function,Loops,Python 3.x,我正在尝试编写一个函数prevword\u ave\u len(word),它接受一个字符串片段word,并返回文本中word前面的单词的平均字符长度 正文是《白鲸》的第一段: 叫我以实玛利。几年前,我的钱包里几乎没有钱,或者根本没有钱,在岸上也没有什么特别让我感兴趣的东西,我想我会航行一段时间,看看世界上有水的地方。这是我驱除脾脏和调节循环的一种方法。每当我发现自己的嘴变得冷酷;每当我的灵魂里有一个潮湿、细雨蒙蒙的十一月;每当我发现自己在棺材仓库前不由自主地停下来,提起我遇到的每一个葬礼的后面

我正在尝试编写一个函数prevword\u ave\u len(word),它接受一个字符串片段word,并返回文本中word前面的单词的平均字符长度

正文是《白鲸》的第一段:

叫我以实玛利。几年前,我的钱包里几乎没有钱,或者根本没有钱,在岸上也没有什么特别让我感兴趣的东西,我想我会航行一段时间,看看世界上有水的地方。这是我驱除脾脏和调节循环的一种方法。每当我发现自己的嘴变得冷酷;每当我的灵魂里有一个潮湿、细雨蒙蒙的十一月;每当我发现自己在棺材仓库前不由自主地停下来,提起我遇到的每一个葬礼的后面;尤其是当我的海波人占据了我的上风,这需要一个强有力的道德原则来防止我故意上街,有条不紊地敲掉别人的帽子——然后,我认为是时候尽快出海了。这是我手枪和球的替代品。卡托带着一种哲学的兴致,扑到了他的剑上;我悄悄地走上船。这并不令人惊讶。如果他们知道这一点,那么几乎所有在他们的学位上的人,无论何时,都会和我对大海怀有几乎相同的感情

有一些特殊要求需要注意:

  • 如果单词恰好是文本中出现的第一个单词,则该出现的前一个单词的长度应计为0
  • 如果文本中没有word,则函数应返回False
  • “单词”只是一个由“空格”分隔的字符串。单词后面的标点符号是单词的一部分
  • 应保留原文和word中的大小写

  • 我该怎么做呢?我的思考过程是将文本分割成一个单词列表,然后使用for循环搜索单词的每个实例,在哪里找到单词,以某种方式将单词索引到单词之前,找到其长度并将其添加到空列表中。然后我将平均这个列表中的元素,然后这将是我的输出。我只是不知道该怎么做?

    这个解决方案使用字典,它的值是前面所有单词长度的列表。 给定的示例打印word
    的解决方案(最后一行)

    如果您不熟悉
    defaultdict
    请查看

    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))