Reputation: 4170
I have data in the two lists value and freq like this:
value freq 1 2 2 1 3 3 6 2 7 3 8 3 ....
and I want the output to be
bin freq 1-3 6 4-6 2 7-9 6 ...
I can write few lines of code to do this. However, I am looking if there are builitin functions in standard python or Numpy? I found the solution when you are given data in array/list with repetition i.e. they are not already grouped into frequency table(eg. d= [1,1,2,3,3,3,6,6,7,7,7,8,8,8,...]
. However, in this case I could not find the answers. I do not want to convert my data into single expanded list like d
first and use histogram function.
Upvotes: 7
Views: 14262
Reputation: 64298
I found the solution when you are given data in array/list with repetition
You didn't say what the solution was, but if it supports taking an iterator, you can generate it, instead of creating the entire list:
import itertools
values = [1,2,3,6]
freqs = [2,1,3,2]
v_iter = itertools.chain(*[ itertools.repeat(v,f) for v, f in zip(values, freqs) ])
#for x in v_iter:
# print x
your_solution(v_iter)
Upvotes: 0
Reputation: 97261
import numpy as np
values = [1,2,3,6,7,8]
freqs = [2,1,3,2,3,3]
hist, _ = np.histogram(values, bins=[1, 4, 7, 10], weights=freqs)
print hist
output:
[6 2 6]
Upvotes: 15
Reputation: 454
you can try this:
import collections
d=[1,1,2,3,3,3,6,6,7,7,7,8,8,8]
collections.Counter([i-i%3+3 for i in d])
it would generate a dictionary with what you want.
Upvotes: 0