【发布时间】:2016-03-04 14:59:15
【问题描述】:
我正在尝试从包含非字母字符的字符串列表中删除单词,例如:
["The", "sailor", "is", "sick", "."] -> ["The", "sailor", "is", "sick"]
但我不能随意删除包含非字母字符的单词,因为可能出现以下情况:
["The", "U.S.", "is", "big", "."] -> ["The", "U.S.", "is", "big"] (acronym kept but period is removed)
我需要想出一个正则表达式或一些类似的方法来处理像这样的简单情况(所有类型的标点符号):
["And", ",", "there", "she", "is", "."] -> ["And", "there", "she", "is"]
我使用一个自然语言包装类将句子转换为左侧的列表,但有时列表要复杂得多:
string: "round up the "blonde bombshells' a all (well almost all)"
list: ["round", "up", "the", "''", "blonde", "bombshell", "\\",
"a", "all", "-lrb-", "well", "almost", "all", "-rrb-"]
如您所见,一些字符(如括号和撇号)被包装器转换或删除。我想把所有这些无关的子串去掉,看起来更干净:
list: ["round", "up", "the", "blonde", "bombshell",
"a", "all", "well", "almost", "all"]
我对python相当陌生,我的印象是正则表达式是我最好的方法,但不知道如何将第一个列表转换为清理后的第二个列表,希望能提供任何帮助!
【问题讨论】: