특정 문자가 나타날 때 문자열을 분할하려고합니다. (예 :!,! etc) split 함수를 작성했지만 분할하지만 그 문자는 제거됩니다. 내가 예를 들어 함수를 호출 할 때 :char에서 split하지만 char - python을 그대로 두십시오.
text = 'The first line leads off, With a gap before the next. Then the poem ends.'
를 내가 얻을
문자가 제거되지 않습니다 때문에 변경할 필요가 무엇['The first line leads off', ' With a gap before the next', ' Then the poem ends']
그래서 난이 얻을 것이다 :
['The first line leads off,', ' With a gap before the next.', ' Then the poem ends.']
을 .
def split_on_separators(original, separators):
word_list = [original]
new_list = [ ]
for given in separators:
for word in word_list:
new_list.extend(word.split(given))
word_list = new_list
new_list = list()
return word_list
감사합니다.
나의 제안이에 대한 자신의 함수를 작성하지 않는 것, 당신이 그것을 달성하기 위해 파이썬 내에서 사용할 수있는 많은 자원이있다. 자세한 내용은 내 대답을 참조하십시오. 그건 그렇고, 당신 Stackoverflow에 대한 답변을 받아 들일 수 있고 더 나은 것들을 받아 들일 수있다;) – mdadm