Reputation: 329
I need to split my words from my punctuation- I'm thinking about having the function look at each word and determining if there is a punctuation mark in it by starting at [-1], index of -1, and then splitting the word from the punctuation as soon as it hits a letter and not a punctuation mark...
sent=['I', 'went', 'to', 'the', 'best', 'movie!','Do','you', 'want', 'to', 'see', 'it', 'again!?!']
import string
def revF(List):
for word in List:
for ch in word[::-1]:
if ch is string.punctuation:
#go to next character and check if it's punctuation
newList= #then split the word between the last letter and first puctuation mark
return newList
Upvotes: 1
Views: 158
Reputation: 172249
Use a regexp containing the punctuation you want to split on:
re.split('re.split(r"[!?.]", text)
Demonstration:
>>> import re
>>> re.split(r"[!?.]", 'bra det. där du! som du gjorde?')
['bra det', ' d\xc3\xa4r du', ' som du gjorde', '']
Upvotes: 0
Reputation:
From your example, I gather that you want to split your string on a space. You can do this quite simply like this:
my_str = "I went to the best movie! Do you want to see it again!?!"
sent = my_str.split(' ')
Upvotes: 0
Reputation: 73618
If all you want is to remove punctuation from a string. Python provides much better ways of doing this -
>>> import string
>>> line
'I went to the best movie! Do you want to see it again!?!'
>>> line.translate(None, string.punctuation)
'I went to the best movie Do you want to see it again'
Upvotes: 1