参考https://blog.csdn.net/qq_43733499/article/details/102722592
求指定层的元素个数
输入一个嵌套列表,再输入层数,求该层的数字元素个数。
输入格式:
第一行输入列表 第二行输入层数
输出格式:
在一行中输出元素个数
输入样例:
在这里给出一组输入。例如:
[1,2,[3,4,[5,6],7],8]
3
输出样例:
在这里给出相应的输出。例如:
2
s = input()
n = int(input())
cur = 0
cnt = 0
for i in range(len(s)):
if s[i] == '[':
cur += 1
elif s[i] == ']':
cur -= 1
if (cur == n)and s[i].isdigit() and s[i+1].isdigit() == False:
cnt += 1
print(cnt)