简体   繁体   English

Python函数:请帮助我

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

okay these two functions are related to each other and fortunately the first one is solved but the other is a big mess and it should give me 17.5 but it only gives me 3 so why doesn't it work out?? 好的,这两个函数是相互关联的,幸运的是第一个函数已解决,但另一个函数很混乱,应该给我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

words can be counted by spliting each sentence in the list using space and counting the length of that list. 可以通过使用空格将列表中的每个句子分开并计算列表的长度来对单词进行计数。 would be helpful. 会有所帮助。

You can eliminate the need for your first function by using regular expressions to split on separators. 您可以通过使用正则表达式拆分分隔符来消除对第一个函数的需求。 The regular expression function is re.split() . 正则表达式函数是re.split() Here is a cleaned up version that gets the right result: 这是一个可以得到正确结果的清理版本:

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)

The one issue with this function is that with the text you provided, it returns 17.0 instead of 17.5. 此功能的一个问题是您提供的文本返回17.0而不是17.5。 This is because there is no space in between "...the Walrus said" and "To talk of..." . 这是因为“ ...海象说”“谈论...”之间没有空格。 There is nothing that can be done there besides adding the space that should be there in the first place. 除了首先增加应​​该存在的空间之外,这里什么也做不了。

If the first function ( split_on_separators ) is required for the project, than you can replace the re.split() function with your function. 如果项目需要第一个函数( split_on_separators ),则可以用函数替换re.split()函数。 Using regular expressions is a bit more reliable and a lot more lightweight than writing an entire function for it, however. 但是,使用正则表达式比为其编写整个函数要可靠一些,并且要轻巧得多。

EDIT 编辑

I forgot to explain the filter() function. 我忘了解释filter()函数。 Basically if you give the first argument of type None , it takes the second argument and removes all "false" items in it. 基本上,如果您给第一个类型为None参数,它将接受第二个参数并删除其中的所有“假”项。 Since an empty string is considered false in Python, it is removed. 由于空字符串在Python中被视为false,因此将其删除。 You can read more about filter() here 您可以在此处阅读有关filter()更多信息

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

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