Here is example how data can be summed to dictionary or you can use numpy.histogram to sum the data as weights of the categorized data.
Putting data to bins
Stackheuw commented: Very helpful +0
data = '''5.639792 1.36
4.844813 1.89
4.809105 2.33
3.954150 2.69
2.924234 3.42
1.532669 4.50
0.000000 5.63
'''
# use the integer part of second value to categorize the first value and add it to that bin
freq = dict()
for d in data.splitlines():
energy, pos = map(float, d.split())
freq[int(pos)] = freq.setdefault(int(pos),0) + float(energy)
print('Categorized by integer part')
print(sorted(freq.items()))
# using numpy.histogram
import numpy
data = [d.split() for d in data.splitlines() if d != '\n']
weights = [float(a) for a,b in data]
pos = [int(float(b)) for a,b in data]
# numpy organizes by itself the limits for bins
print('5 bins by numpy histogram')
print(numpy.histogram(pos,bins=5, weights=weights))
Gribouillis 1,391 Programming Explorer Team Colleague
TrustyTony commented: usufull idiom .strip().splitlines() +13
TrustyTony 888 pyMod Team Colleague Featured Poster
lrh9 95 Posting Whiz in Training
TrustyTony 888 pyMod Team Colleague Featured Poster
Stackheuw 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.