python - average of certain values of an array -


i have array contains numbers distances, , represents values @ distance. how calculate average of data @ fixed value of distance?

e.g distances (d): [1 1 14 6 1 12 14 6 6 7 4 3 7 9 1 3 3 6 5 8]

e.g data corresponding entry of distances:

therefore value=3.3 @ d=1; value=2,1 @ d=1; value=3.5 @ d=14; etc..

[3.3 2.1 3.5 2.5 4.6 7.4 2.6 7.8 9.2 10.11 14.3 2.5 6.7 3.4 7.5 8.5 9.7 4.3 2.8 4.1]

for exampe @ distance d=6 should mean of 2.5, 7.8, 9.2 , 4.3

i want values of d appear in vector 'd' , create vector (or matrix) of averages corresponding distance.

thank in advance magnificent help!

this handles every case in lists. adjust needed.

key = [1, 1, 14, 6, 1, 12, 14, 6, 6, 7, 4, 3, 7, 9, 1, 3, 3, 6, 5, 8] dist = [3.3, 2.1, 3.5, 2.5, 4.6, 7.4, 2.6, 7.8, 9.2, 10.11, 14.3, 2.5, 6.7, 3.4, 7.5, 8.5, 9.7, 4.3, 2.8, 4.1]  d in set(key):     choose = [dist[i] in range(len(key)) if key[i] == d]     print d, float(sum(choose)) / len(choose) 

you can shorten code bit more numpy:

from numpy import mean  d in set(key):     print d, mean([dist[i] in range(len(key)) if key[i] == d]) 

output:

1 4.375 3 6.9 4 14.3 5 2.8 6 5.95 7 8.405 8 4.1 9 3.4 12 7.4 14 3.05 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -