2

假设以下文本:

# Algorithms
This chapter covers the most basic algorithms.
## Sorting
Quicksort is fast and widely used in practice
Merge sort is a deterministic algorithm
## Searching
DFS and BFS are widely used graph searching algorithms
# Data Structures
more text
## more data structures

我将如何在 python 中创建一个目录,如果该行以#我将替换1.为第一个#。如果该行以##I开头,1.1.则在文本中第二次替换为 a#会看到我需要替换为2.,依此类推:

1. Algorithms
1.1. Sorting
1.2. Searching
2. Data Structures
2.1 more data structures

我会开始做类似的事情:

for line in text:
 if line.startswith('#'):
      ....

但我不知道如何进行。

4

3 回答 3

2

你可以这样做:

from collections import defaultdict

levels = defaultdict(int)
max_level = 0
index = []
for line in text:
    if not line.startswith("#"):
        continue
    hashes, caption = line.rstrip().split(maxsplit=1)
    level = len(hashes)
    max_level = max(level, max_level)
    levels[level] += 1
    for l in range(level + 1, max_level + 1):
        levels[l] = 0
    index.append(
        ".".join(str(levels[l]) for l in range(1, level+1))
        + ". " + caption
    )

一些扩展:

  • levels存储目录计数器的当前状态。这是defaultdict(int)因为它的值是ints 并且这使得它易于使用。
  • max_level存储到目前为止计数器的最大深度。(例如:在从单个#s 到第一个的步骤中##,最大深度增加 ( 1->2)。)
  • 如果一行文本以开头#,则将其分成两部分:1. 散列和 2. 标题。
  • 哈希数 ( len(hashes)) 是当前条目的级别深度。以防万一它增加了迄今为止的最大深度,max_level获取更新(通常什么都不做)。
  • 然后当前级别的计数器增加,并且当前级别之外的1所有计数器都重置为。(例如:如果计数器的最后一个状态是,然后第一级增加(),不仅计数器必须切换第一个位置(),而且第二级需要重置()。另一个是删除那些水平。01.2#1->22->0
  • 中的输出包含直到当前join级别的所有单个计数器(例如,获取而不是等)1.3.1.3.0.0.0.0.

结果为

from io import StringIO
text = StringIO(
'''
# Algorithms
This chapter covers the most basic algorithms.
## Sorting
Quicksort is fast and widely used in practice
Merge sort is a deterministic algorithm
## Searching
DFS and BFS are widely used graph searching algorithms
# Data Structures
more text
## more data structures
''')

['1. Algorithms',
 '1.1. Sorting',
 '1.2. Searching',
 '2. Data Structures',
 '2.1. more data structures']

另一种但类似的方法defaultdict是:

levels = []
index = []
for line in text:
    if not line.startswith("#"):
        continue
    hashes, caption = line.rstrip().split(maxsplit=1)
    level = len(hashes)
    if level > len(levels):
        levels.append(1)
    else:
        levels[level-1] += 1
    levels = levels[:level]
    index.append(
        ".".join(str(l) for l in levels) + ". " + caption
    )
于 2021-10-25T08:30:06.437 回答
1

您可以使用递归:

from collections import defaultdict
def to_table(d, p = []):
   r, c, l = defaultdict(list), 0, None
   for a, *b in d:
      if a != '#' and p:
         yield f'{".".join(map(str, p))} {"".join(b)}'
      elif a == '#':
         r[l:=((c:=c+1) if b[0] != '#' else c)].append(''.join(b))
   yield from [j for a, b in r.items() for j in to_table(b, p+[a])]

s = """
# Algorithms
This chapter covers the most basic algorithms.
## Sorting
Quicksort is fast and widely used in practice
Merge sort is a deterministic algorithm
## Searching
DFS and BFS are widely used graph searching algorithms
# Data Structures
more text
## more data structures
"""
print('\n'.join(to_table(list(filter(None, s.split('\n'))))))

输出:

1 Algorithms
1.1 Sorting
1.2 Searching
2 Data Structures
2.1 more data structures
于 2021-10-25T12:55:56.850 回答
0

您可以跟踪顶级编号和子级别编号,并将单哈希替换为顶级编号,将双哈希替换为格式top.sub

你可以参考这个

于 2021-10-25T14:04:43.393 回答