【问题标题】:How to split a string by numbering?如何通过编号拆分字符串?
【发布时间】:2018-12-22 18:56:01
【问题描述】:

我希望将以下语料库拆分为多个部分:

corpus = '1  Write short notes on the anatomy of the Circle of Willis including normal variants.     2  Write short notes on the anatomy of the radiological spaces of the orbit excluding the eyeball.      3  Write short notes on the anatomy of the axis (C2 vertebra).      4  Write short notes on the anatomy of the corpus callosum.      5  Write short notes on the anatomy of the posterior division of the internal iliac artery  6  Write short notes on the anal canal including sphincters.               
      '

如下:

['Write short notes on the anatomy of the Circle of Willis including normal variants.', 'Write short notes on the anatomy of the radiological spaces of the orbit excluding the eyeball.', 'Write short notes on the anatomy of the axis (C2 vertebra).', 'Write short notes on the anatomy of the posterior division of the internal iliac artery', 'Write short notes on the anal canal including sphincters.']

我写了这个,但不起作用:

for i in [int(s) for s in corpus.split() if s.isdigit()]:
    answer = corpus.split(str(i))

print(answer)

我能做什么?

【问题讨论】:

  • “它不起作用”可能是一个正确的描述,但如何它不起作用?
  • 如果我知道 - 我不会问你...
  • 但是您确实知道运行它时会发生什么。它会给出错误吗?它会打印出垃圾吗?你的电脑关机了吗?

标签: python regex string nltk


【解决方案1】:

对于您的示例数据,您还可以匹配零次或多次空格,后跟一位或多位数字和 2 次空格以匹配 split on:

*\d+

print (filter(None, re.split(' *\d+  ', corpus)))

Demo

为了清楚起见,您可以将空格放在字符类中,后跟量词 [ ]*\d+[ ]{2}

【讨论】:

    【解决方案2】:

    您标记了,但提供了非正则表达式解决方案。这是您的 OP 的非正则表达式正确解决方案。

    分割空格是可以的,然后将文本部分累积到一个临时变量中,直到遇到下一个数字,然后将临时添加到您的整体结果中。

    由于不可变性,使用列表存储临时(部分)比附加到字符串更有效。

    跳过存储数字本身:

    corpus = '1  Write short notes on the anatomy of the Circle of Willis including normal variants.     2  Write short notes on the anatomy of the radiological spaces of the orbit excluding the eyeball.      3  Write short notes on the anatomy of the axis (C2 vertebra).      4  Write short notes on the anatomy of the corpus callosum.      5  Write short notes on the anatomy of the posterior division of the internal iliac artery  6  Write short notes on the anal canal including sphincters.'               
    
    allparts = []  # total result
    part = []      # parts that belong to one number
    for p in corpus.split():
        if p.isdigit():      # if a number
            if part:             # if stored something
                allparts.append(' '.join(part))   # add it to result
                part=[]
            continue         # skip storing the number  
    
        part.append(p)      # add to part
    
    if part:   # add rest
        allparts.append(' '.join(part))
    
    print(allparts)
    

    输出:

    ['Write short notes on the anatomy of the Circle of Willis including normal variants.', 
     'Write short notes on the anatomy of the radiological spaces of the orbit excluding the eyeball.', 
     'Write short notes on the anatomy of the axis (C2 vertebra).', 
     'Write short notes on the anatomy of the corpus callosum.', 
     'Write short notes on the anatomy of the posterior division of the internal iliac artery', 
     'Write short notes on the anal canal including sphincters.']
    

    【讨论】:

    • DV - 没有评论。如何知道我应该改进什么?解决方案有效,所以不可能。
    • 您好:您的代码的圈复杂度(5 跟在lizard metrix 之后)比必要的要高得多,而且总的来说,您的代码远非 Pythonic。
    • @LucaCappelletti - 感谢您的反馈。我的是一个可行的解决方案,它很容易理解,它解决了手头的问题。使用正则表达式的 oneliner 将产生所需的输出 - 我怀疑它是否会提供很多学习的机会。如果某些指标告诉我我的指标更复杂,那么我必须不同意。
    【解决方案3】:

    使用re.split 和列表推导,使用str.strip 删除最终的空格:

    import re
    result = [
        phrase for phrase in map(str.strip, re.split('\d+\s\s', corpus)) if phrase
    ]
    

    结果:

    ['Write short notes on the anatomy of the Circle of Willis including normal variants.',
     'Write short notes on the anatomy of the radiological spaces of the orbit excluding the eyeball.',
     'Write short notes on the anatomy of the axis (C2 vertebra).',
     'Write short notes on the anatomy of the corpus callosum.',
     'Write short notes on the anatomy of the posterior division of the internal iliac artery',
     'Write short notes on the anal canal including sphincters.']
    

    【讨论】:

    • 解释否决票是个好习惯,这样答案可能会更好。
    【解决方案4】:

    尝试使用 re.split() 和正则表达式 + strip()

    a = "1  hello.  2  my name is. 3  maat."
    
    answer = [s.strip(" ") for s in filter(None, re.split(" *\d+ ", a))]
    
    print(answer) #['hello.', 'my name is.', 'maat.']
    

    re.split() 几乎是 split() 但它也包含分隔符 / strip(" ") 从 s 中删除空格

    【讨论】:

    • 当数字的值大于 9 并且返回空字符串时,您的答案不包括在内,因为它在运行过滤器后最后执行 strip。
    • 感谢您的意见,但我不明白您评论的最后一部分。如果你不介意的话,能解释一下吗?
    • 当然:当您有一个字符串 " " 并运行 .strip 时,您会返回一个空列表 ““
    • 另外,您的方法在(C2 vertebra).的用户示例中拆分
    • 我已编辑。感谢您的细致反馈!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 2021-12-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    相关资源
    最近更新 更多