在python里,如何用list的方法将一个string按给定的特殊符号断开,将断开的元素存放为一个list。我给出具体题目,我在国外学习,所以题目是英文的,大家见谅。defsplit_on_separators...
在python里,如何用list的方法将一个string按给定的特殊符号断开,将断开的元素存放为一个list。我给出具体题目,我在国外学习,所以题目是英文的,大家见谅。
def split_on_separators(original, separators):
''' 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.'''
#To do. complete this function to meet the specification
# you are not required to keep the two lines below but you may find
# them helpful. (Hint)
result = [original]
return result
大意:从原来的string中return一个非空的string的数列(list),在list中的string是由原string被用户输入的特定分隔符分开的。这就是大体意思,翻译水平烂,但是自己看的懂,没有调整语序,大家见谅
我附上一个例子split_on_separators("Hooray! Finally, we're done.",'!,') should return ['Hooray', ' Finally', " we're done."]
以下三楼我全部测试过了,如果我的separators是‘!,’就是有2个符号连在一起并且用但引号扩出,那么你们的方法最后return的时候就不对了,要像我给的例子一样
展开