python - How should I be able to conisder exclamation mark as different sentence separator beside just full stop -
i practicing test , wrote code seems not consider !
mark sentence sperator work if use either.
for sep in (".", "!", "?") sentences = s.split(sep) num_of_sentences = len(sentences) # print num_of_sentences tempdict = {} i, sent in enumerate(sentences): tempdict[i] = len(sent.split()) print sentences return tempdict[max(tempdict.iteritems(), key=operator.itemgetter(1))[0]]
the objective return maximum number of word out of sentence. print sol2("this one. , two. serious? hi!")
should output 4
your code isn't working because returns after first loop iteration, in return
keyword. never run "!"
.
have considered using regular expressions this?
import re s = 'this one. , two. serious? hi!' sentences = re.findall(r'(.*?)[!.?]', s) # split sentences print(sentences) # ['this one', ' , two', ' serious', ' hi']
Comments
Post a Comment