python:如何解决此错误?

时间:2014-03-07 07:33:47

标签: python

我不知道该怎么处理这个功能......第一个问题解决但另一个问题一团糟我只得到3而不是17.5

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= words+1
    sentence=split_on_separators(text,'?!.')
    for sep in sentence:
        Sentences+=1

    ASL=words/Sentences
    return ASL

3 个答案:

答案 0 :(得分:0)

您收到此错误的原因是以下代码行 -

 for index,char in enumerate(original)

你正在迭代原始的,这是一个列表而不是字符串而不是字符,

char in separators

永远不会是真的

len(original)

是2,所以你在2次迭代后停止。您需要迭代文本中的每个字符串。

答案 1 :(得分:0)

他们分开的方式是错误的。你是从列表中计算元素数而不是单词。

Sentences=0
words = len(filter(None, re.split("[\n\r\s:\?.,-;]+", " ".join(text))))
##                     if you need  ^^^^^^^^^^^^^ add more word separators here
sentence=split_on_separators(" ".join(text),'?!.')
for sep in sentence:
    Sentences+=1
ASL=float(words)/Sentences
print ASL

答案 2 :(得分:0)

这两个功能都有问题。因此,不要重新发布相同的问题,而是在代码中进行一些改进。做一些调试,看看问题出在哪里?为什么你也没有得到预期的输出?

import re

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

    for line in text:
        # be careful here, kind of hard coded!
        line_values = list(filter(("-").__ne__, line.strip().split()))
        words += len(line_values)
    # I didn't call your function as that is erroneus
    #sentence=split_on_separators(''.join(text),'?!.')

    sentence =[x for x in re.compile('[.!?]').split(''.join(text).strip()) if x]    
    ASL=words/len(sentence)
    return ASL

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']
print (average_sentence_length(text))