Op 11 mei 2018 21:43 schreef Neil Cerutti <ne...@norwich.edu>:
On 2018-05-10, Albert-Jan Roskam <sjeik_ap...@hotmail.com> wrote: > If a punctuation symbol is in your string: Replace that symbol > with an empty string. > >=>>> maybe something like > > import re > no_interpunction = re.sub("[%s]" % re.escape(string.punctuation), '', > sentence) str.translate can be used instead of re. # Compute only once somewhere punctuation_removal_table = str.maketrans({c: None for c in string.punctuation}) no_interpunction = sentence.translate(punctuation_removal_table) Unfortunately you'll remove all the apostrophes and dashes, both of which could be considered parts of words. Nice, I did not think of str.translate. It appears to be the fastest (and most readable) method, though a compiled regex is also pretty fast: http://bbs.bugcode.cn/t/3320#IDb95ffe2791c1a59f0ac8175905705f34 _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor