繁体   English   中英

Python函数:请帮助我

[英]Python function: Please help me in this one

好的,这两个函数是相互关联的,幸运的是第一个函数已解决,但另一个函数很混乱,应该给我17.5,但只能给我3,所以为什么不起作用?

def split_on_separators(original, separators):
    """ (str, str) -> list of str

    Return a list of non-empty, non-blank strings from the original string
    determined by splitting the string on any of the separators.
    separators is a string of single-character separators.

    >>> split_on_separators("Hooray! Finally, we're done.", "!,")
    ['Hooray', ' Finally', " we're done."]
    """
    result = []
    newstring = ''

    for index,char in enumerate(original):
        if char in separators or index==len(original) -1:
            result.append(newstring)
            newstring=''
            if '' in result:
                result.remove('')
        else:
            newstring+=char
    return result

def average_sentence_length(text):
    """ (list of str) -> float

    Precondition: text contains at least one sentence. A sentence is defined
    as a non-empty string of non-terminating punctuation surrounded by 
    terminating punctuation or beginning or end of file. Terminating 
    punctuation is defined as !?.

    Return the average number of words per sentence in text.   

    >>> text = ['The time has come, the Walrus said\n',
         'To talk of many things: of shoes - and ships - and sealing wax,\n',
         'Of cabbages; and kings.\n'
         'And why the sea is boiling hot;\n'
         'and whether pigs have wings.\n']
    >>> average_sentence_length(text)
    17.5
    """
    words=0
    Sentences=0
    for line in text:
        words+=1
    sentence=split_on_separators(text,'?!.')
    for sep in sentence:
        Sentences+=1

    ASL=words/Sentences
    return ASL

可以通过使用空格将列表中的每个句子分开并计算列表的长度来对单词进行计数。 会有所帮助。

您可以通过使用正则表达式拆分分隔符来消除对第一个函数的需求。 正则表达式函数是re.split() 这是一个可以得到正确结果的清理版本:

import re

def average_sentence_length(text):

    # Join all the text into one string and remove all newline characters
    # Joining all text into one string allows us to find the sentences much
    # easier, since multiple list items in 'text' could be one whole sentence
    text = "".join(text).replace('\n', '')

    # Use regex to split the sentences at delimiter characters !?.
    # Filter out any empty strings that result from this function,
    # otherwise they will count as words later on
    sentences = filter(None, re.split('[!?.]', text))

    # Set the word sum variable
    wordsum = 0.0

    for s in sentences:
            # Split each sentence (s) into its separate words and add them
            # to the wordsum variable
            words = s.split(' ')
            wordsum += len(words)

    return wordsum / len(sentences)


data = ['The time has come, the Walrus said\n',
     ' To talk of many things: of shoes - and ships - and sealing wax,\n',
     'Of cabbages; and kings.\n'
     'And why the sea is boiling hot;\n'
     'and whether pigs have wings.\n']

print average_sentence_length(data)

此功能的一个问题是您提供的文本返回17.0而不是17.5。 这是因为“ ...海象说”“谈论...”之间没有空格。 除了首先增加应​​该存在的空间之外,这里什么也做不了。

如果项目需要第一个函数( split_on_separators ),则可以用函数替换re.split()函数。 但是,使用正则表达式比为其编写整个函数要可靠一些,并且要轻巧得多。

编辑

我忘了解释filter()函数。 基本上,如果您给第一个类型为None参数,它将接受第二个参数并删除其中的所有“假”项。 由于空字符串在Python中被视为false,因此将其删除。 您可以在此处阅读有关filter()更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM