2014-03-07 98 views
0

好吧這兩個函數是相互關聯的,幸運的是第一個解決了,但另一個是一個大亂,它應該給我17.5,但它只給了我3爲什麼不它解決了嗎?Python函數:請幫我在這一個

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 
+1

'對於文本行:單詞+ = 1'?這不包括單詞的數量。 – Blender

+0

另外,'split_on_separators'接受一個字符串,'text'是一個列表。 – icedtrees

+0

是的我知道,對於文本中的行:單詞+ = 1不會計算單詞的數量......這就是問題所在。我怎樣才能讓它計算單詞的數量? – user3283844

回答

0

可以通過使用空格拆分列表中的每個句子並計算該列表的長度來計算單詞。會有幫助。

0

通過使用正則表達式拆分分隔符,可以消除對第一個函數的需求。正則表達式函數是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。這是因爲之間沒有空間「...... Walrus說」「要說......」。除了增加應該在那裏的空間之外,沒有什麼可以做的。

如果項目需要第一個功能(split_on_separators),則可以使用函數替換re.split()函數。然而,使用正則表達式比爲它編寫整個函數要可靠得多,而且要輕得多。

編輯

我忘了解釋filter()功能。基本上,如果你給出None類型的第一個參數,它將接受第二個參數並刪除其中的所有「false」項目。由於Python中的空字符串被認爲是錯誤的,因此它被刪除。您可以閱讀更多關於filter()here