【问题标题】:regex to remove non alpha-words A-Z a-z from a list (exceptions)正则表达式从列表中删除非字母单词 A-Z a-z(例外)
【发布时间】: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相当陌生,我的印象是正则表达式是我最好的方法,但不知道如何将第一个列表转换为清理后的第二个列表,希望能提供任何帮助!

【问题讨论】:

    标签: python regex list


    【解决方案1】:

    这似乎符合您的描述:

    cases=[
        ["The", "sailor", "is", "sick", "."],
        ["The", "U.S.", "is", "big", "."],
        ["round", "up", "the", "''", "blonde", "bombshell", "\\", 
        "a", "all", "-lrb-", "well", "almost", "all", "-rrb-"],
    ]
    
    import re
    
    for li in cases:
        print '{}\n\t->{}'.format(li, [w for w in li if re.search(r'^[a-zA-Z]', w)])
    

    打印:

    ['The', 'sailor', 'is', 'sick', '.']
        ->['The', 'sailor', 'is', 'sick']
    ['The', 'U.S.', 'is', 'big', '.']
        ->['The', 'U.S.', 'is', 'big']
    ['round', 'up', 'the', "''", 'blonde', 'bombshell', '\\', 'a', 'all', '-lrb-', 'well', 'almost', 'all', '-rrb-']
        ->['round', 'up', 'the', 'blonde', 'bombshell', 'a', 'all', 'well', 'almost', 'all']
    

    如果正确,您完全可以不用正则表达式:

    for li in cases:
        print '{}\n\t->{}'.format(li, [w for w in li if w[0].isalpha()])
    

    【讨论】:

    • 我不必打印列表(只需返回)。只是想知道以下内容是否足以解决案例。抱歉,我对 python 很陌生,我知道这是列表理解,所以这似乎是正确的 li = [w for w in li if w[0].isalpha()])
    • 那么你会做def f(li): return [w for w in li if w[0].isalpha()]) -- 完成。
    【解决方案2】:

    您可以使用punctuation 来执行此操作:

    >>> from string import punctuation
    >>> [i for i in lst if i not in punctuation]   
    ['The', 'U.S.', 'is', 'big']
    

    【讨论】:

      【解决方案3】:

      通过确保每个字符串至少包含一个字母数字:

      import re
      
      expr = re.compile(r"\w+")
      test = ["And", ",", "there", "she", "is", ".", "U.S."]
      
      filtered = [v for v in test if expr.search(v)]
      print(filtered)
      

      打印

      ['And', 'there', 'she', 'is', 'U.S.']
      

      替代方案将排除数字,并确保字符串不以非字母字符开头:

      # only alpha
      expr = re.compile(r"[a-zA-Z]+")
      test = ["round", "up", "the", "''", "blonde", "bombshell", "\\",
              "a", "all", "-lrb-", "well", "almost", "all", "-rrb-"]
      # use match() here
      filtered = [v for v in test if expr.match(v)]
      print(filtered)
      

      打印

      ['round', 'up', 'the', 'blonde', 'bombshell', 'a', 'all', 'well', 'almost', 'all']
      

      【讨论】:

      • 不适用于最后一个字符串 ["round", "up", "the", "''", "blonde", "bombshel​​l", "\\", "a" , "all", "-lrb-", "well", "almost", "all", "-rrb-"]
      • 在 Python 3.5 上工作得很好
      • close 但这不适用于金发碧眼的重磅炸弹案例,我使用的是 python 2.7,因为包装类是特定于 python 2 的,不幸的是它不能在 python3 中完成
      猜你喜欢
      • 2016-03-04
      • 2011-04-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 2011-06-22
      相关资源
      最近更新 更多