簡體   English   中英

Python - 從列表中刪除特殊字符

[英]Python - Remove Special Characters from list

我有一個單詞列表,我想刪除所有特殊字符和數字,這是我想出的:

輸入:#將所有單詞轉換為小寫

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'。 有任何想法嗎?

如果要刪除所有非字母字符,請嘗試:

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

您可以使用內置快捷方式,而不必指定所有特殊字符。 這是一種刪除除“單詞字符”之外的所有內容的方法:

進口重新

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 個字符到字符串的末尾”。

更新:我剛剛注意到你說你不想要結果中的數字。 那么我猜你只想要字母。 我改變了表達來做到這一點。 這就是正則表達式的好處。 您可以調整被替換的內容,而無需添加額外的調用、循環等,而只需更改字符串值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM