简体   繁体   English

python比较字典中的元素到float

[英]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. 如果我有一个pvalues字典,那么访问pvalues[i][classes[j]]可以将类j与行i中的其他类进行比较,从而得到pvalue。 However, when I try to iterate over the dictionary to find the significant values (ie, if pvalues[i][classes[j]] < 0.05 ), I get TypeError: unorderable types: list() < float() . 但是,当我尝试遍历字典以查找有效值时(即,如果pvalues[i][classes[j]] < 0.05 ), TypeError: unorderable types: list() < float()得到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? 无论如何,有没有将列表元素与0.05浮点数进行比较而不使列表变平且不丢失类名称的情况? Thanks. 谢谢。

As you mentioned in your comment, Your pvalues[i][classes[j]] is actually giving a list which is then being compare to float. 正如您在评论中提到的那样,您的pvalues [i] [classes [j]]实际上给出了一个列表,然后将其与float进行比较。 You should compare the first element of that list and the float instead : 您应该将列表的第一个元素与float进行比较:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM