Python-从列表中删除特殊字符

时间:2020-10-03 19:14:27

标签: python list special-characters

我有一个单词列表,我想删除所有特殊字符和数字,这是我的构想:

输入: #将所有单词转换为小写

words = [word.lower() for word in words]
print(words[:100])

输出:

['rt', '@', 'dark', 'money', 'has', 'played', 'a', 'significant', 'role', 'in', 'the', 'overall', 'increase', 'of', 'election', 'spending', 'in', 'state', 'judicial', 'elections.', 'https://e85zq', 'rt', '@', 'notice,', 'women,', 'how', 'you', 'are', 'always', 'the', 'target', 'of', 'democrats’', 'fear', 'mongering', 'in', 'an', 'election', 'year', 'or', 'scotus', 'confirmation.', 'it', 'is', 'not', 'because', 'our', 'rights', 'are', 'actually', 'at', 'risk.', 'it', 'is', 'because', 'we', 'are', 'easily', 'manipulated.', 'goes', 'allll', 'the', 'way', 'back', 'to', 'eve.', 'resist', 'hysteria', '&', 'think.', 'rt', '@', 'oct', '5:', 'last', 'day', 'to', 'register', 'to', 'vote.', 'oct', '13:', 'early', 'voting', 'starts.', 'oct', '23:', 'last', 'day', 'to', 'request', 'a', 'mail-in', 'ballot.', 'nov', '3:', 'election', 'day', 'rt', '@']

输入

words_cleaned = [re.sub(r"[-()\"#/@;:<>{}`+=~|.!?,]", "", i) for i in words]

print(words_cleaned[:100])

输出

我最后得到一个空字符串[]

我需要的是像'@'这样的字符被删除,像'@test'这样的字符变成'test'。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

如果要删除所有非字母字符,请尝试:

words = ["".join(filter(lambda c: c.isalpha(), word)) for word in words]
print(words)

答案 1 :(得分:2)

您可以使用内置的快捷方式,而不必指定所有特殊字符。这是删除“文字字符”以外的所有内容的方法:

重新导入

inp = ['rt', '@', 'dark', 'money', 'has', 'played', 'a', '#significant', 'role', 'in', 'tRhe', 'overall', 'increase', 'of', 'election', 'spending', 'in', 'state', 'judicial', 'elections.', 'https://e85zq', 'rt', '@', 'notice,', 'women,', 'how', 'you', 'are', 'always', 'the', 'target', 'of', 'democrats’', 'fear', 'mongering', 'in', 'an', 'election', 'year', 'or', 'scotus', 'confirmation.', 'it', 'is', 'not', 'because', 'our', 'rights', 'are', 'actually', 'at', 'risk.', 'it', 'is', 'because', 'we', 'are', 'easily', 'manipulated.', 'goes', 'allll', 'the', 'way', 'back', 'to', 'eve.', 'resist', 'hysteria', '&amp;', 'think.', 'rt', '@', 'oct', '5:', 'last', 'day', 'to', 'register', 'to', 'vote.', 'oct', '13:', 'early', 'voting', 'starts.', 'oct', '23:', 'last', 'day', 'to', 'request', 'a', 'mail-in', 'ballot.', 'nov', '3:', 'election', 'day', 'rt', '@']

outp = [re.sub(r"[^A-Za-z]+", '', s) for s in inp]

print(outp)

结果:

['rt', '', 'dark', 'money', 'has', 'played', 'a', 'significant', 'role', 'in', 'tRhe', 'overall', 'increase', 'of', 'election', 'spending', 'in', 'state', 'judicial', 'elections', 'httpse85zq', 'rt', '', 'notice', 'women', 'how', 'you', 'are', 'always', 'the', 'target', 'of', 'democrats', 'fear', 'mongering', 'in', 'an', 'election', 'year', 'or', 'scotus', 'confirmation', 'it', 'is', 'not', 'because', 'our', 'rights', 'are', 'actually', 'at', 'risk', 'it', 'is', 'because', 'we', 'are', 'easily', 'manipulated', 'goes', 'allll', 'the', 'way', 'back', 'to', 'eve', 'resist', 'hysteria', 'amp', 'think', 'rt', '', 'oct', '5', 'last', 'day', 'to', 'register', 'to', 'vote', 'oct', '13', 'early', 'voting', 'starts', 'oct', '23', 'last', 'day', 'to', 'request', 'a', 'mailin', 'ballot', 'nov', '3', 'election', 'day', 'rt', '']

这里的^字符表示匹配[]对内的字符集中未提及的所有字符。 \w的意思是“文字字符” 。因此,整个过程都说“匹配单词字符以外的所有字符”。使用正则表达式的好处是,您可以任意精确地确定要包含或排除的字符。

无需使用[:100对结果进行切片以进行打印。像我一样按原样打印。我假设通过使用100,您要确保自己进入列表的末尾。更好的方法是将该组件留空。因此,[:]的意思是“截取完整字符串的一部分”,而[5:]的意思是“从第6个字符开始到字符串的末尾。”

更新:我刚刚注意到您说您不希望结果中出现数字。因此,我想您只想要字母。我更改了表达式来做到这一点。这就是正则表达式的优点。您可以调整替换的内容而无需添加其他调用,循环等,而只需更改字符串值即可。

相关问题