Allie H Allie H - 1 year ago 69
Python Question

python comparing elements in dictionary to a float

If I have a dictionary of pvalues, such that accessing

pvalues[i][classes[j]]
gives the pvalue from comparing class j to other classes in row i. However, when I try to iterate over the dictionary to find the significant values (i.e., if
pvalues[i][classes[j]] < 0.05
), I get
TypeError: unorderable types: list() < float()
. Is there anyway to compare the list elements to the 0.05 float without flattening the list and losing the class designations? Thanks.

Answer Source

As you mentioned in your comment, Your pvalues[i][classes[j]] is actually giving a list which is then being compare to float. You should compare the first element of that list and the float instead :

if pvalues[i][classes[j]][0] < .5 :
    print("it worked!!!")