需求如下图:

代码如下:Python3.10,运行程序闪退
请修改直接成可以直接使用
with open('data.txt') as f:
string = f.read()lines = string.split('\n')
result = []
for line in lines:
count = 0dist = []last_index = 0num_arr = line.split(' ')for index, num in enumerate(num_arr):if eval(num) > 0:count += 1if last_index:dist.append(str(index - last_index))last_index = indexelse:last_index = indexresult.append({'count': count , 'dist': dist})for res in result:
print(f'{res["count"]}个 {" ".join(res["dist"])}')todo: output to TXT file
python IDLE执行下,检查下错误输出