2014-03-07 68 views
0
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."] 
    """ 



    # To do: Complete this function's body to meet its specification. 
    # You are not required to keep the two lines below but you may find 
    # them helpful. (Hint) 
    result = [original] 
    return result 
+0

看起來像在這裏一樣的... http://stackoverflow.com/questions/22241667/i-cant-make-the-function-completely-correct/22241936?noredirect=1#comment33777537_22241936 – faboolous

回答

0

這就是我解決它的方法。

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 = [original] 
     for sep in separators: 
      r = [] 
      for sub in result: 
       r.extend(sub.split(sep)) 
      result = r 
     return result 
相關問題