-1

I have a list:

AllProcessTimes = [ [[1,2,3], [1,2,3], [1,2,3]],
                    [[1,2,3], [1,2,3], [1,2,3]],
                    [[1,2,3], [1,2,3], [1,2,3]],
                    [[1,2,3], [1,2,3], [1,2,3]],
                    [[2,4,6], [3,6,9], [4,8,12]] ]

I want to calculate an average of each column so that the final result for this example would be:

AverageProcessTimes = [ [1.2, 2.4, 3.6], [1.4, 2.8, 4.2], [1.6, 3.2, 4.8] ]

How can I do this without creating a load of placeholder lists?

Billy
  • 19
  • 3
  • 1
    Although the duplicate talks about a 2d array, it is equally applicable to a 3d array – Nick Dec 07 '20 at 00:49

2 Answers2

1

Use numpy's mean method. Numpy is very efficient and speed optimized library.

import numpy as np

AllProcessTimes = np.array([ [[1,2,3], [1,2,3], [1,2,3]],
                    [[1,2,3], [1,2,3], [1,2,3]],
                    [[1,2,3], [1,2,3], [1,2,3]],
                    [[1,2,3], [1,2,3], [1,2,3]],
                    [[2,4,6], [3,6,9], [4,8,12]] ])

AverageProcessTimes = np.mean(AllProcessTimes, axis=0)
pip1726
  • 173
  • 1
  • 9
0

Try this, but modify it a bit for your use.

from statistics import mean 
 
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88] 
list_avg = mean(inp_lst) 
 
print("Average value of the list:\n") 
print(list_avg) 
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(list_avg,3))
Chinceapizza
  • 1
  • 1
  • 1
  • I want to avoid putting all of the 1st, 2nd and 3rd values of each foundation list into another list and calculating an average if possible. The method you recommend would require me to do this. – Billy Dec 07 '20 at 00:45