将级别深度转换为从属计数

时间:2011-02-14 01:47:20

标签: python parsing tree

让我们考虑这个示例文本文件:

Level: 1
Level: 1
Level: 2
Level: 2
Level: 2
Level: 3
Level: 3
Level: 1
Level: 2
Level: 2
Level: 1

我需要使用提供的级别数据将这些行转换为从属计数(不包括下属的下属) 结果应如下所示:

:0
:3
:0
:0
:2
:0
:0
:2
:0
:0
:0

我的第一个想法是将文本文件转换为XML并使用简单的xml解析器技术作为解决方案,但后来我记得python有时可以做出神奇的事情,并且如果有人知道如何以“pythonic”方式做到这一点,则会想到寻求建议(没有xml帮助)

由于

1 个答案:

答案 0 :(得分:1)

可能的解决方案:

input_text = """Level: 1
Level: 1
Level: 2
Level: 2
Level: 2
Level: 3
Level: 3
Level: 1
Level: 2
Level: 2
Level: 1"""

# Parse input text into a list of levels (as int)
levels = []
for line in input_text.split('\n'):
    try:
        levels.append(int(line.split(': ', 1)[-1]))
    except Exception:
        pass # skip line

# Compute how many children each node has by counting the number of
# consecutive subsequent lines that belong to exactly the next level
counters = [0] * len(levels)
for i, level in enumerate(levels):
    for other in levels[i + 1:]:
        if other == level + 1:
            counters[i] += 1
        elif other == level:
            break

print '\n'.join(':%d' % c for c in counters)