Home > Back-end >  How to count an element that exceed our criteria in a list that is in a list that is also in a list
How to count an element that exceed our criteria in a list that is in a list that is also in a list

Time:05-06

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

CodePudding user response:

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 Trues where value greater than 5 and Falses 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)

CodePudding user response:

I'm not sure of why this data structure was choosen but it look simpler to me to just flatten the list and sub list in a single list with all elements then perform operations on it :

def flatten(l):
    res = []
    for el in l:
        if type(el) == list:
            #If it's a list then we extract all the elements in it
            res.extend(flatten(el))
        else:
            #Only add in the result the non-list elements
            res.append(el)
    return res

dex = [[[[0.00173012],
         [0.0009075 ],
         [0.00080378],
         [0.00069336],
         ....
         [0.00074539],
         [0.00186453]]]]

flatten_dex = flatten(dex) 
#Here flatten_dex is [0.00173012, 0.0009075, 0.00080378, 0.00069336, ..., 0.00074539, 0.00186453]

Once you have this list it's much simple to count the number of elements matching the condition :

nb_elements_greater_than_0_8 = len([e for e in flatten_dex if e > 0.8])
number_of_total_elements = len(flatten_dex)
  • Related