Comparing values from a multidimentional array using Python -


i have array of numbers:

a=  [[0, 0.1667],  [1, 0.1667],  [2, 0.25],   [3, 0.25],  [4, 0.167]]. 

each pair contains numbers , proportions.

i want compare particular number z resulted proportions. lets assume, z=0.02. have iterate through proportions , compare each proportion z , find number proportion falls below z , print number only.

i think, proportions need sorted highest lowest first , has compared z.

sorting helpful if you're dealing lots of data, can loop until first element bigger z , you're done.

however, easy way going through elements using list comprehension:

a = [[0, 0.1667],  [1, 0.1667],  [2, 0.25],   [3, 0.25],  [4, 0.167]]  z = 0.2 print [x x in if x[1] < z] 

this iterate through elements in a , check if second number of each element in a less z, if yes, added new list.

output:

>>>  [[0, 0.1667], [1, 0.1667], [4, 0.167]] 

(i chose z = 0.2, because z = 0.02 list empty :) )


Comments