【问题标题】:Split a sentence on capital letters用大写字母拆分句子
【发布时间】:2018-03-16 20:28:50
【问题描述】:

如何拆分?

'Symptoms may include:Absent or small knucklesCleft palateDecreased skin creases at finger jointsDeformed earsDroopy eyelidsInability to fully extend the joints from birth (contracture deformity)Narrow shouldersPale skinTriple-jointed thumbs'

期望的输出应该采用这种形式

Symptoms may include:
Absent or small knuckles
Cleft palate
Decreased skin creases at finger joints
Deformed ears
Droopy eyelids
Inability to fully extend the joints from birth (contracture deformity)
Narrow shoulders
Pale skin
Triple-jointed thumbs

就像拆分大写字母。

【问题讨论】:

  • 您是否已经尝试过以某种方式解决它?分享你的尝试。 Stackoverflow 不是“为我编写”服务。

标签: python string split


【解决方案1】:

使用re.findall(感谢@Brendan Abel 和@JFF 改进了模式):

fragments = re.findall('[A-Z][^A-Z]*', text)

print(fragments)
['Symptoms may include:',
 'Absent or small knuckles',
 'Cleft palate',
 'Decreased skin creases at finger joints',
 'Deformed ears',
 'Droopy eyelids',
 'Inability to fully extend the joints from birth (contracture deformity)',
 'Narrow shoulders',
 'Pale skin',
 'Triple-jointed thumbs']

详情

[A-Z]      # match must begin with a uppercase char
[^A-Z]*    # further characters in match must not contain an uppercase char

注意:* 可让您捕获包含单个大写字符的句子。如果这不是所需的功能,请替换为 +

另外,如果您希望输出为多行字符串:

print('\n'.join(fragments))

【讨论】:

  • 你可以避免前瞻,只测试非大写字母[A-Z][^A-Z]+
  • 该死!我差点打败了,我只是复制粘贴而已 ;-) +1
  • @BrendanAbel 谢谢,这是一个很大的改进。
  • 如何拆分“新一期”?
  • @cᴏʟᴅsᴘᴇᴇᴅ 酒吧,我很愚蠢:您以前的模式有效,因为空格与大写否定匹配。但是正则表达式的贪婪使得新模式也能正常工作
【解决方案2】:
>>> s = 'Symptoms may include:Absent or small knucklesCleft palateDecreased skin creases at finger jointsDeformed earsDroopy eyelidsInability to fully extend the joints from birth (contracture deformity)Narrow shouldersPale skinTriple-jointed thumbs'
>>> print(''.join(('\n' + c if c.isupper() else c) for c in s)[1:])
Symptoms may include:
Absent or small knuckles
Cleft palate
Decreased skin creases at finger joints
Deformed ears
Droopy eyelids
Inability to fully extend the joints from birth (contracture deformity)
Narrow shoulders
Pale skin
Triple-jointed thumbs

工作原理

  • (('\n' + c if c.isupper() else c) for c in s)

    上面会生成字符串s 中每个字符c 的列表,除非c 是大写,在这种情况下,它会在该字符前面添加一个新行。

  • ''.join(('\n' + c if c.isupper() else c) for c in s))

    这会将列表重新组合成一个字符串。

  • ''.join(('\n' + c if c.isupper() else c) for c in s)[1:]

    这会从字符串的开头删除多余的换行符。

【讨论】:

    【解决方案3】:

    我觉得下面的代码可能很有趣

    import re
    output = re.sub( r"([A-Z])", r"\n\1", inputString)
    print(output)
    

    您也可以通过拆分所有 \n 将其存储回列表中

    outputList = output.split('\n')[1::]
    

    这最初将所有大写字母替换为\n,然后是大写字母

    【讨论】:

    • @Jean-FrançoisFabre 你测试过吗?..(提示:它确实有效...ideone.com/ybk5t3)........
    • 我的错,我太自信了,是的,它有效。但是您使用的是正则表达式,then 拆分,then 切片。我不建议这样做。
    • output = re.sub( r"([A-Z])", r"\n\1", inputString).split('\n')[1:] 不幸的是需要一个 sub + split 调用才能工作(更不用说,前面的空字符串)。
    • @Yes..它比 findall 慢了近 3.7 倍,但我正试图找到另一种出路。但让我告诉你,时间复杂度由re.sub( r"([A-Z])", r"\n\1", inputString) 主导,而不是分裂或切片。
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2019-08-08
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多