缩进和取消缩进线的算法

问题描述 投票:0回答:1

我有一份很长的文档,其中的文字如下:

paragraph = '''
• Mobilising independently with a 4-wheel walker
• Gait: Good quality with good cadence, good step length, and adequate foot clearance
• Lower limb examination:
- Tone: Normal bilaterally
- No clonus in either leg
- Passive dorsiflexion: Possible to -10 to -5 degrees bilaterally
- Power: 5/5 in all major muscle groups of lower limbs bilaterally
- Sensation: Intact to gross touch bilaterally
- ROM:
o Right hip flexion: 0-120 degrees
o Left hip flexion: 10-120 degrees (fixed flexion deformity present)
• Feet: Not broad-based

2. Post-stroke mobility and spasticity
• Improvement noted in right leg stiffness
• Continuing with current exercise regimen:
- Neuro Group twice weekly
- Gym group (including bike riding)
- Home exercises
• Plan:
- Continue current exercise programme
- Maintain current baclofen dose

'''

我希望通过正确的缩进来正确格式化行。基本上我希望将上面的内容转换为:

paragraph = '''
• Mobilising independently with a 4-wheel walker
• Gait: Good quality with good cadence, good step length, and adequate foot clearance
• Lower limb examination:
  - Tone: Normal bilaterally
  - No clonus in either leg
  - Passive dorsiflexion: Possible to -10 to -5 degrees bilaterally
  - Power: 5/5 in all major muscle groups of lower limbs bilaterally
  - Sensation: Intact to gross touch bilaterally
  - ROM:
    o Right hip flexion: 0-120 degrees
    o Left hip flexion: 10-120 degrees (fixed flexion deformity present)
• Feet: Not broad-based

2. Post-stroke mobility and spasticity
• Improvement noted in right leg stiffness
• Continuing with current exercise regimen:
  - Neuro Group twice weekly
  - Gym group (including bike riding)
  - Home exercises
• Plan:
  - Continue current exercise programme
  - Maintain current baclofen dose

'''

我编写了以下代码,但它没有正确格式化字符串:

add_indent = ""; corpus = []; bullet_point = ""
for line in paragraph.split("\n"):
    if line.strip().endswith(":") and len(line.split(" ")[0])==1: add_indent += "  "; bullet_point = line.split(" ")[0]
    elif not line.strip().endswith(":") and bullet_point == line.split(" ")[0]: add_indent = add_indent[:-2]
    elif not line: add_indent = ""
    corpus.append(add_indent+line)

for line in corpus: print(line)

我哪里出错了?

python string
1个回答
0
投票

首先,只是一些友好的建议,不要像其他语言一样格式化你的Python,除非必须,否则不要引入分号。查看您的代码:

add_indent = ""
corpus = []
bullet_point = ""
for line in paragraph.split("\n"):
    if line.strip().endswith(":") and len(line.split(" ")[0]) == 1:
        add_indent += "  "
        bullet_point = line.split(" ")[0]
    elif not line.strip().endswith(":") and bullet_point == line.split(" ")[0]:
        add_indent = add_indent[:-2]
    elif not line:
        add_indent = ""
    corpus.append(add_indent + line)

for line in corpus: 
    print(line)

当您的代码在末尾检测到

add_indent
时,它会增加当前缩进
:
,但随后将其应用于当前正在分析的行,这会导致缩进过早地开始一行。

更一般地说,您会寻找

:
,但检测项目符号样式的变化不是更有意义吗?

关于:

result = []
indent = 0
indent_size = 2
bullet_points = []
for line in paragraph.split("\n"):
    if not line:
        result.append(line)
        continue
    first = line.split()[0]
    # this may be a bit weak, consider predefined valid bullet point characters
    if len(first) == 1:
        if first in bullet_points:
            bullet_points = bullet_points[:bullet_points.index(first) + 1]
        else:
            bullet_points.append(first)
        indent = (len(bullet_points) - 1) * indent_size
    result.append(' ' * indent + line.strip())

for line in result:
    print(line)
© www.soinside.com 2019 - 2024. All rights reserved.