从列表中删除标点符号

Remove punctuations from a list

我有这个:

words = ["Alice's", 'Adventures', 'in', 'Wonderland', "ALICE'S", 'ADVENTURES', 'IN', 'WONDERLAND', 'Lewis', 'Carroll', 'THE', 'MILLENNIUM', 'FULCRUM', 'EDITION', '3.0', 'CHAPTER', 'I', 'Down', 'the', 'Rabbit-Hole', 'Alice', 'was']

remove_strings = str.maketrans('                           ', '!*01.23456,789-\,?\'\.(:;)\"!')

words = [s.translate(remove_strings) for s in words]
words = [words.lower() for words in words]

我想去掉所有的标点符号和数字。

但它只是转换为小写,并没有像我想象的那样删除标点符号。

我做错了什么?

str.maketrans 将第一个参数中指定的字符映射到第二个参数,因此您实际上只是将 space 映射到当前代码中的不同字符。因此,快速解决方法是简单地交换两个参数:

remove_strings = str.maketrans('!*01.23456,789-\,?\'\.(:;)\"!', '                           ')

一种更简单的方法是使用正则表达式替换将所有非字母表替换为 space:

import re

words = [re.sub('[^a-z]', ' ', word, flags=re.I).lower() for word in words]