[Tutor] Iteration issues

Albert-Jan Roskam sjeik_appie at hotmail.com
Fri May 11 22:22:41 EDT 2018

Op 11 mei 2018 21:43 schreef Neil Cerutti <neilc at norwich.edu>:

On 2018-05-10, Albert-Jan Roskam <sjeik_appie at 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

More information about the Tutor mailing list