python-使用1行代码删除句子列表中的标点符号

add*_*ted 2 python string

所以我有一个句子列表,我想从每个句子中删除标点符号。我可以这样删除它:

textList = ['This is bad.', 'You, me, him are going']

from string import punctuation

for text in textList:
    for p in punctuation:
        text = text.replace(p,'')
    print(text)
Run Code Online (Sandbox Code Playgroud)

但是我想修改列表内容,然后一行完成。像这样:

# obviously this does not work
textList = [(text.replace(p,'') for p in punctuation) for text in textList]
Run Code Online (Sandbox Code Playgroud)

正确的做法是什么?

ett*_*any 5

在Python 2中,您可以str.translate()像下面这样使用:

res = [s.translate(None, string.punctuation) for s in textList]
Run Code Online (Sandbox Code Playgroud)

输出:

>>> textList = ['This is bad.', 'You, me, him are going']
>>> res = [s.translate(None, string.punctuation) for s in textList]
>>> res
['This is bad', 'You me him are going']
Run Code Online (Sandbox Code Playgroud)

在Python 3中,您可以像这样使用str.maketrans()

res = [s.translate(str.maketrans('', '', string.punctuation)) for s in textList]
Run Code Online (Sandbox Code Playgroud)

注意:使用您的方法,您可以执行以下操作:

res = []

for text in textList:
    new_text = ''.join(c for c in text if c not in string.punctuation)
    res.append(new_text)
Run Code Online (Sandbox Code Playgroud)

一行:

res = [''.join(c for c in text if c not in string.punctuation) for text in textList]
Run Code Online (Sandbox Code Playgroud)