我的星星图案如下
*
*
**
***
***
**
***
*
**
***
基于上述模式,我必须生成如下所示的编号索引。
1
2
2.1
2.1.1
2.1.2
2.2
2.2.1
3
3.1
3.1.1
我正在尝试使用循环,但是找不到如何生成子索引。我正在尝试使用python。 我可以使用任何库或算法来实现这一目标。
答案 0 :(得分:3)
简单的generator function:在每次迭代中,将索引列表移至星星的长度,然后增加最后一个索引。
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']
答案 1 :(得分:2)
假设您的模式位于文件"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))
说明:
输出
1
2
2.1
2.1.1
2.1.2
2.2
2.2.1
3
3.1
3.1.1
答案 2 :(得分:2)
假设星形模式存储在字符串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)))
这将输出:
1
2
2.1
2.1.1
2.1.2
2.2
2.2.1
3
3.1
3.1.1
答案 3 :(得分:1)
您可以从行中建立字典,并在解析的每一行之后打印整个字典数字:
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}
答案 4 :(得分: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
2
2.1
2.1.1
2.1.2
2.2
2.2.1
3
3.1
3.1.1