Python 带星计数的聚集数索引

Python 带星计数的聚集数索引,python,Python,我有下面的星星图案 * * ** *** *** ** *** * ** *** 基于上述模式,我必须生成如下所示的编号索引 1 2 2.1 2.1.1 2.1.2 2.2 2.2.1 3 3.1 3.1.1 我正在尝试使用循环,但无法找到如何生成子索引。我正在尝试python。 是否有任何库或算法可用于实现此目的。假设您的模式位于文件“tempfile”中,您可以使用以下代码: #!/usr/bin/env python with open("tempfile") as f: c

我有下面的星星图案

*
*
**
***
***
**
***
*
**
***
基于上述模式,我必须生成如下所示的编号索引

1
2
2.1
2.1.1
2.1.2
2.2
2.2.1
3
3.1
3.1.1
我正在尝试使用循环,但无法找到如何生成子索引。我正在尝试python。
是否有任何库或算法可用于实现此目的。

假设您的模式位于文件
“tempfile”
中,您可以使用以下代码:

#!/usr/bin/env python

with open("tempfile") as f:
    content=f.readlines()
content = [x.strip() for x in content]
arr=[0,0,0,0]
for line in content:
    arr[len(line)-1] = arr[len(line)-1]+1
    arr[len(line):]=[0] * (len(arr)-len(line))
    tarr=arr[:len(line)]
    print ".".join(map(str,tarr))
说明:

  • 将所有元素保留为0的内存数组
  • 对于每一行,计算星号的数量
  • 对于星号为n的行,将索引为n-1的内存数组增加1
  • 将所有索引向右分配,值为0
  • 对所有行重复逻辑
输出

1
2
2.1
2.1.1
2.1.2
2.2
2.2.1
3
3.1
3.1.1

您可以根据您的行建立字典,并在解析每行后打印完整的dicts编号:

text = """*
*
**
***
***
**
***
*
**
***"""

d = {}
for l in text.splitlines():

    # add key if needed (with 0) 
    d.setdefault(l,0)
    # increment key
    d[l] += 1

    lenL = len(l)
    # delete any key in the dict that is longer then the actual one
    delete = [k for k in d.keys() if len(k) > lenL]
    for k in delete:
        del d[k]

    # from 3.7 on dict insert order is guaranteed - you could skip the sorting
    # sort keys by length and print theire respective values
    all_keys = sorted(d.keys(), key=len) 
    print('.'.join( ( str(d[k]) for k in all_keys) ), 
          "\t                from ", d) # this line just to display the dict
输出:

1                       from  {'*': 1}
2                       from  {'*': 2}
2.1                     from  {'*': 2, '**': 1}
2.1.1                   from  {'*': 2, '**': 1, '***': 1}
2.1.2                   from  {'*': 2, '**': 1, '***': 2}
2.2                     from  {'*': 2, '**': 2}
2.2.1                   from  {'*': 2, '**': 2, '***': 1}
3                       from  {'*': 3}
3.1                     from  {'*': 3, '**': 1}
3.1.1                   from  {'*': 3, '**': 1, '***': 1}    
1
2
2.1
2.1.1
2.1.2
2.2
2.2.1
3
3.1
3.1.1
一个简单的方法是:在每次迭代中,将索引列表调整到星号的长度,然后增加最后一个索引

def gen(stars):
    inds = []
    for star in stars:
        if len(star) > len(inds):  # length should not increase by more than one ...
            inds.append(0)
        while len(star) < len(inds):  # ... but can decrease by more
            inds.pop()
        inds[-1] += 1
        yield '.'.join(map(str, inds))

>>> stars = ['*', '**', '***', '**', '***', '***', '***', '*', '**', '***']
>>> list(gen(stars))
['1', '1.1', '1.1.1', '1.2', '1.2.1', '1.2.2', '1.2.3', '2', '2.1', '2.1.1']
def gen(星型):
inds=[]
对于星星中的星星:
如果len(星形)>len(inds):#长度不应增加一个以上。。。
inds.append(0)
而len(star)>>星=['*'、'**'、'***'、'***'、'***'、'***'、'***'、'***'、'*'、'***'、'***'、'***']
>>>名单(一般(星级))
['1', '1.1', '1.1.1', '1.2', '1.2.1', '1.2.2', '1.2.3', '2', '2.1', '2.1.1']

还可以查看和上为方便起见使用的文档。

您可以创建一个计数列表,用于跟踪每个级别。增加当前级别,并重置其旁边的所有级别

stars = ['*', '*', '**', '***', '***', '**', '***', '*', '**', '***']
counts = [] # to keep track of level counts
numbered_index = [] # will contain the numbered index

for s in stars:
    # if there are more levels than already known, initialize them with 0
    counts = counts + [0] * (len(s) - len(counts))
    # increment the current level count
    counts[len(s) - 1] += 1
    # the counts up to current level contain the index, join them in a string
    numbered_index.append('.'.join(map(str, counts[:len(s)])))
    # set all the values ahead of current level to zero, as the current level has been increased
    counts[len(s):] = [0] * (len(counts) - len(s))

print('\n'.join(numbered_index))
输出:

1                       from  {'*': 1}
2                       from  {'*': 2}
2.1                     from  {'*': 2, '**': 1}
2.1.1                   from  {'*': 2, '**': 1, '***': 1}
2.1.2                   from  {'*': 2, '**': 1, '***': 2}
2.2                     from  {'*': 2, '**': 2}
2.2.1                   from  {'*': 2, '**': 2, '***': 1}
3                       from  {'*': 3}
3.1                     from  {'*': 3, '**': 1}
3.1.1                   from  {'*': 3, '**': 1, '***': 1}    
1
2
2.1
2.1.1
2.1.2
2.2
2.2.1
3
3.1
3.1.1

假设您的星形模式存储在string
s
中,这里有一种有效的方法:

i = [0]
for l in s.splitlines():
    while len(i) < len(l):
        i.append(0)
    while len(l) < len(i):
        i.pop()
    i[-1] += 1
    print('.'.join(map(str, i)))

你的母函数是什么还不清楚。显示您正在使用的函数以及您遇到的任何错误。