2021年3月30日星期二

Calculating average and s.d from a list of tuples

I have a list of tuples called routers that looks like this:

('142.104.68.167', 11.111999999999853)  ('142.104.68.167', 11.369000000000142)  ('142.104.68.167', 11.618999999999915)  ('142.104.68.1', 16.60699999999997)  ('142.104.68.1', 16.847999999999956)  ('142.104.68.1', 17.097000000000207)  ('192.168.9.5', 15.727999999999838)  ('192.168.9.5', 16.01800000000003)  ('192.168.9.5', 16.279999999999973)  

I have more entries in the list but for now this should be enough. I want to calculate the mean and standard deviation of the values that have the same "key", for example, calculate the average and s.d of all values who's "key" is 142.104.68.167, then calculate the average and s.d of all values who's "key" is 142.104.68.1 and so on.

I have tried doing it in this manner but it is incorrect

for i in range(len(routers)):          for j in range(len(routers)):              if (routers[i][0] == routers[j][0]):                  if ((routers[i][0] not in final_router_list) and (routers[j][0] not in final_router_list)):                      final_router_list.append(routers[i][0])    sum = 0  for i in range(len(routers)):      for j in range(len(final_router_list)):          if (routers[i][0] == final_router_list[j]):              sum = sum + routers[i][1]              print(routers[i][0],"rout:",final_router_list[j],"time:",routers[i][1],"sum:",sum)  

This is the output that I get:

142.104.68.167 rout: 142.104.68.167 time: 11.111999999999853 sum: 11.111999999999853  142.104.68.167 rout: 142.104.68.167 time: 11.369000000000142 sum: 22.480999999999995  142.104.68.167 rout: 142.104.68.167 time: 11.618999999999915 sum: 34.09999999999991  142.104.68.1 rout: 142.104.68.1 time: 16.60699999999997 sum: 50.70699999999988  142.104.68.1 rout: 142.104.68.1 time: 16.847999999999956 sum: 67.55499999999984  142.104.68.1 rout: 142.104.68.1 time: 17.097000000000207 sum: 84.65200000000004  192.168.9.5 rout: 192.168.9.5 time: 15.727999999999838 sum: 100.37999999999988  192.168.9.5 rout: 192.168.9.5 time: 16.01800000000003 sum: 116.39799999999991  192.168.9.5 rout: 192.168.9.5 time: 16.279999999999973 sum: 132.67799999999988  

What I want it to be is:

142.104.68.167 rout: 142.104.68.167 time: 11.111999999999853 sum: 11.111999999999853  142.104.68.167 rout: 142.104.68.167 time: 11.369000000000142 sum: 22.480999999999995  142.104.68.167 rout: 142.104.68.167 time: 11.618999999999915 sum: 34.09999999999991  142.104.68.1 rout: 142.104.68.1 time: 16.60699999999997 sum: 16.60699999999997  142.104.68.1 rout: 142.104.68.1 time: 16.847999999999956 sum: 33.455  142.104.68.1 rout: 142.104.68.1 time: 17.097000000000207 sum: 50.552  192.168.9.5 rout: 192.168.9.5 time: 15.727999999999838 sum: 15.727999999999838  192.168.9.5 rout: 192.168.9.5 time: 16.01800000000003 sum: 31.746  192.168.9.5 rout: 192.168.9.5 time: 16.279999999999973 sum: 40.026  
https://stackoverflow.com/questions/66880080/calculating-average-and-s-d-from-a-list-of-tuples March 31, 2021 at 08:29AM

没有评论:

发表评论