【问题标题】:Python - Remove Special Characters from listPython - 从列表中删除特殊字符
【发布时间】:2020-10-03 19:14:27
【问题描述】:

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

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

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'。有什么想法吗?

【问题讨论】:

  • @PranavHosangadi:我可以建议阅读正则表达式吗? . 在字符类 [ ... ] 中时不表示任何字符。
  • 你好@andrew-seaman,你的代码在我的笔记本上完美运行。您可以再次尝试您的代码或在此处上传您的整个代码吗?
  • @LukeWoodward 那不是我脸上的鸡蛋! OP,您的代码工作正常。请附上完整的minimal reproducible example 以重现您的问题
  • ''.join(e for e in string if e.isalpha())

标签: python list special-characters


【解决方案1】:

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

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

【讨论】:

    【解决方案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 个字符到字符串的末尾”。

    更新:我刚刚注意到您说您不希望结果中出现数字。那么我猜你只是想要字母。我改变了表达来做到这一点。这就是正则表达式的好处。您可以调整被替换的内容,而无需添加额外的调用、循环等,而只需更改字符串值。

    【讨论】:

    • 并非如此。正如 Luke 在 cmets 中指出的那样,字符类 [...] 中的 . 不需要转义。 OP 的代码运行良好。
    • 发生了一些变化。我没有它的副本,但是当我第一次运行他的代码时,我发誓我得到了一个空输出,他说他也得到了。
    • 可能是我还认为.首先需要转义的原因吗?偷偷摸摸的OP!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    相关资源
    最近更新 更多