nums = [10, 20, 30, 40, 50, 60]
sum = 0
avg = 2 # 以二數平均為例
lst = []
for i, element in enumerate(nums):
if (i+1) % avg != 0:
sum += element
else:
lst.append(sum/avg)
sum = 0
a = 1
with open('1.txt','w') as f:
for i in lst:
f.write(str((a-1)*avg+1)+'~'+str(a*avg)+' ' +str(i)+'\n')
a += 1
結果如下:
1~2 5.0
3~4 15.0
5~6 25.0