简体   繁体   中英

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

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() . 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. 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. 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. Basically if you give the first argument of type None , it takes the second argument and removes all "false" items in it. Since an empty string is considered false in Python, it is removed. You can read more about filter() here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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