I have a seemingly simple issue. Consider this list:
a = [[12.0, 5.0, 63.0], [0.1, 2.0, 7.1, 3.0, 2.3, 5.0, 8.4]]
I want to find the mean (using
numpy) of all the elements in its sublists combined. In this case the result should be:
np.mean([12.0, 5.0, 63.0, 0.1, 2.0, 7.1, 3.0, 2.3, 5.0, 8.4])
The solution I've found is to flatten the list first and then obtain the mean, as:
np.mean([item for sublist in a for item in sublist])
but this seems unnecessarily complicated. I would've assumed that
numpy.mean() could handle this case without the need to modify the list first. I tried using the argument
axis to no avail.
Am I missing some functionality here?