【问题标题】:Remove content with parentheses under multiple conditions in Python在Python中的多个条件下删除带括号的内容
【发布时间】:2021-10-28 22:44:42
【问题描述】:

给定一个列表如下:

l = ['hydrogenated benzene (purity: 99.9 density (g/cm3), produced in ZB): SD', 
    'Car board price (tax included): JT Port', 
    'Ex-factory price (low-end price): Triethanolamine (85% commercial grade): North'
    ]

我想得到预期的结果如下:

['hydrogenated benzene: SD', 'Car board price: JT Port', 'Ex-factory price: Triethanolamine: North']

代码如下:

def remove_extra(content):
    pat1 = '[\s]'  # remove space
    pat2 = '\(.*\)' # remove content within parentheses
    combined_pat = r'|'.join((pat2, pat3))
    return re.sub(combined_pat, '', str(content))
[remove_extra(item) for item in l]

它生成:

['hydrogenated benzene : SD',
 'Car board price : JT Port',
 'Ex-factory price : North']

您可能会注意到,结果'Ex-factory price : North' 的最后一个元素与预期的不一样,我怎么能达到我所需要的?谢谢。

【问题讨论】:

  • 可以使用a = [re.sub(r'\((?:[^)(]|\([^)(]*\))*\)', '', str(item)) for item in l] 吗? : 之前是否需要删除空格?
  • 从结果中看起来很有意义。你的意思是使用'\((?:[^)(]|\([^)(]*\))*\)' 作为pat1[\s] 作为pat2
  • 我测试了[\s] 并删除了所有空格,看来这不是您需要的。

标签: python-3.x pandas re


【解决方案1】:

内括号使它变得复杂。您在此处看到的解决方案适用于您的示例,但可能不适用于您的整个数据集。如果您遇到错误,请更新问题,以便我们找到解决方案。

此函数首先计算字符串中存在多少个单独的括号,然后将其删除。

def par_remover(st):
    begin = [ i.start() for i in re.finditer('\(', st)]
    end = [ i.start() for i in re.finditer('\)', st)]
    count = len(list(re.finditer('\(', st))) +1 - len([i for i in begin if i < end[0]])
    for i in range(count):
        begin = [ i.start() for i in re.finditer('\(', st)]
        end = [ i.start() for i in re.finditer('\)', st)]
        end1 = len([i for i in begin if i < end[0]])
        str_remove = st[st.find("("):list(re.finditer('\)', st))[end1-1].end()]
        st = st.replace(str_remove,'')
    return(st.replace(')',''))

df = pd.DataFrame({'value':l})

df['value'] = df['value'].apply(lambda st:par_remover(st))

结果:

|    | value                                      |
|---:|:-------------------------------------------|
|  0 | hydrogenated benzene : SD                  |
|  1 | Car board price : JT Port                  |
|  2 | Ex-factory price : Triethanolamine : North |

【讨论】:

  • 谢谢,但列表最后一个元素的预期结果应该是Ex-factory price: Triethanolamine: North,而不是Ex-factory price: North
【解决方案2】:

问题实际上不是您的第三项,而是第一项,因为存在嵌套括号。你应该做一个这样的循环并使用subn 而不是sub

def remove_text_between_parens(text):
    n = 1
    while n:
        text, n = re.subn(r'\s*\([^()]*\)\s*', '', text)
    return text
>>> [remove_text_between_parens(t) for t in l]
['hydrogenated benzene: SD',
 'Car board price: JT Port',
 'Ex-factory price: Triethanolamine: North']

正确的解释在这里:https://stackoverflow.com/a/37538815/15239951

【讨论】:

    【解决方案3】:

    您可以使用\s* 修改链接解决方案以删除( 之前的可选空格:

    #https://stackoverflow.com/a/37538815/2901002 
    def remove_text_between_parens(text):
        n = 1  # run at least once
        while n:
            text, n = re.subn(r'\s*\([^()]*\)', '', text) #remove non-nested/flat balanced parts
        return text
    
    a = [remove_text_between_parens(item) for item in l]
    print (a)
    
    ['hydrogenated benzene: SD', 
     'Car board price: JT Port', 
     'Ex-factory price: Triethanolamine: North']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-21
      • 2012-12-05
      • 1970-01-01
      • 2022-11-03
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      相关资源
      最近更新 更多