If you go with numpy and your 3D array is a numpy array, this single line file will do the trick:
numpy.where(your_array_name != 0, 1, 0).sum()
Example:
In [23]: import numpy In [24]: a = numpy.array([ [[0, 1, 2], [0, 0, 7], [9, 2, 0]], [[0, 0, 0], [1, 4, 6], [9, 0, 3]], [[1, 3, 2], [3, 4, 0], [1, 7, 9]] ]) In [25]: numpy.where(a != 0, 1, 0).sum() Out[25]: 18
Todd minehardt
source share