how do I round by n groups in python?

79 views Asked by At

I am working on a way to create anonymus data. Therefore I want to to some sort of rounding my data. But this should happen in n-groups that have the same range. minimal group should be min(a) and from there on it goes in n steps to max(a)-diff(max(a),min(a))/n

example 1:

a = [10,11,14,15,16.5,17,19]
round_n_groups(x=a,n_groups=5)

returns:
[10,10,14,14,16,16,18]

example 2:

b = [-.5,.1,.7,1.3]
round_n_groups(x=b,n_groups=2)

returns:
[-.5,-.5,.4,.4]
2

There are 2 answers

0
finedominos On

I would go naivey with 2 for loops. One that creates the group of possible values with your formula "max(a)-diff(max(a),min(a))/n", and then I would loop through your input x and attribute the closed value it finds in the group of possible values..

0
Jarod L On

Here's how the round function works:

x = 2.5
y = round(x,1)
z = round(x,2)
print(y)
print(z)

Hope this helps you understand.