说我有以下txt文件
Distances Counts
1 5
2 5
3 9
4 10
9 10
10 10
11 5
14 5
20 1
21 1
23 2
我想根据第一列进行分箱并将第二列相加。 如果使用bin为5的正确输出将是
0-5 29
5-10 20
10-15 10
15-20 20
20-25 3
或只是
5 29
10 20
15 10
20 20
25 3
我试过
binfile = open('distances.txt', 'r')
binsize = 5
summar = 0
binsize2 = binsize
for line in binfile:
line = line.strip().split('\t')
distance = int(line[0])
counts = int(line[1])
if distance <= binsize2:
summar += counts
else:
print(str(binsize2)+'\t'+str(summar))
binsize2 = binsize2 + binsize
summar = counts
但它不打印最后一个bin。有什么建议吗?