如果可能的话,不要重新发明轮子。NumPy具有您所需的一切:
#!/usr/bin/env pythonimport numpy as npa = np.fromfile(open('file', 'r'), sep='n')# [ 0. 0.005 0.124 0. 0.004 0. 0.111 0.112]# You can set arbitrary bin edges:bins = [0, 0.150]hist, bin_edges = np.histogram(a, bins=bins)# hist: [8]# bin_edges: [ 0. 0.15]# Or, if bin is an integer, you can set the number of bins:bins = 4hist, bin_edges = np.histogram(a, bins=bins)# hist: [5 0 0 3]# bin_edges: [ 0. 0.031 0.062 0.093 0.124]


