I'm trying to count how many element that is exceed some criteria (for exemple: 0.7) and then convert them into percent, the element is in a multiple list that look like this:
[[[[0.00173012]
[0.0009075 ]
[0.00080378]
...
[0.00069336]
[0.00074539]
[0.00186453]]
[[0.00081442]
[0.00022855]
[0.00019197]
...
[0.00018318]
[0.00017222]
[0.00075811]]
[[0.00084458]
[0.00020444]
[0.0001783 ]
...
[0.00020849]
[0.00017066]
[0.00070635]]
...
[[0.00073932]
[0.00022051]
[0.00024553]
...
[0.00028661]
[0.00019603]
[0.0007242 ]]
[[0.00085666]
[0.0002345 ]
[0.00021651]
...
[0.0002319 ]
[0.00017067]
[0.00066847]]
[[0.00188439]
[0.00092146]
[0.00082662]
...
[0.00077084]
[0.00066442]
[0.00178707]]]]
info: there is ...
because it is a long list and cant fit all the list in the output cell (it is originally an image)
I've tried using:
len(pex > 0.7)/100
#pex is variable contain the multiple list above
but it's not really working because the ouput from the len is just 1, and if i divide it by 100 the output will be just 0.01
Is there any way for me to easily count all the element and the element that exceed some criteria so i can convert them into percent?? TIA
If you are allowed to use numpy
this can be easily done, consider following example
import numpy as np
data = [[[1,2],[3,4]],[[5,6],[7,8]]]
arr = np.array(data) # create numpy.array
print(np.sum(arr>5)) # count elements > 5
print(arr.size) # number of all elements
output
3
8
Explanation: convert nested lists into numpy.array
use comparison to get same-shaped array with True
s where value greater than 5 and False
s elsewhere, then use numpy.sum
(not built-in sum
function) to get count, as True
and False
are treated as 1
and 0
when subjected to arithmetic operations (this also apply outside numpy
, e.g. sum([True,True,True])
gives 3
)