Jan-22-2020, 04:44 PM
Corrected, now it works if the data sequence started with higher value than the bin size.
def make_bins(data, bin_size): binned_data = {} #variable to store the output bin_low = 0 #initial lower limit of the bin bin_high = bin_size #initial high limit of the bin key = str(bin_low) + '-' + str(bin_high) #variable stores the key name i = 1 #counter for the values of the floats fits in the actal bin for data_piece in data: #iter over the sorted data while data_piece < bin_low: bin_low += bin_size bin_high += bin_size if bin_low <= data_piece < bin_high: #if the current data fits the bin, lower inclusive and upper exclusive binned_data[key] = i #append the counter for the key i += 1 #increment the key else: # if the current data doesn't fit the bin i = 1 #reset counter to 1 bin_low += bin_size bin_high += bin_size #and increment the bin limits by the bin size key = str(bin_low) + '-' + str(bin_high) # generate a new key binned_data[key] = i #and append it return(binned_data)