sorted()函数与通过列表推导获得的结果不同

时间:2018-01-16 00:04:42

标签: python sorting floating-point

我似乎遇到了Python内置sorted函数的错误。我似乎无法在任何地方找到它记录或描述的内容,但是根据我对Python的理解,我也得到了看似根本不可能的结果,因此我希望有人可以提供帮助,或者在至少指出我正确的方向。

首先,我有一长串花车(确切地说是1.4米)。它们都非常小,有些非常接近。实际上当投射到一组时,长度只有~240k。

我的目标很简单:我需要找到一个截止值,这样只有n值高于此截止值,看起来很简单。这是我目前的代码:

sorted_values = sorted(all_values)
top_250 = [item for item in sorted_values if item < sorted_values[250]]
len(top_250)  # 5828

不幸的是,数据是专有的(并且相当大),所以我无法分享,但希望我已经给出了足够的细节。该列表是所有浮点数的列表,全部在0和1之间,有许多重复。我不能为我的生活弄清楚这里发生了什么。

我最好的猜测是sorted比较器的运行方式与Python中的传统<不同。也许<有一个隐含的epsilon与之相关,我不知道,但我在Python文档中找不到类似的东西,所以我真的走到了尽头

如果有人甚至对这可能是最模糊的感觉,我将非常感激。

更新:如果我将all_values替换为list(set(all_values)),则此问题将消失,sorted将按预期执行。然而,这并没有解决我的问题,也没有解释为什么sorted的行为与记录不同。基于在本地再现这种情况的大量尝试(和失败),这似乎特别适用于大数据大小的浮点数。

更新2:以下数据,并在此要点:https://gist.github.com/Slater-Victoroff/bc03fb39e07a30caf57460330d02d3f7

[4.8502882289326867e-14, 8.820403952763587e-14, 1.0250590153123516e-13, 1.6954166246183968e-13, 1.908753789966549e-13, 2.004836996365545e-13, 3.5909784960471013e-13, 5.166693869401628e-13, 7.550572170701337e-13, 9.448695131203987e-13, 1.0156517689399418e-12, 1.1252632977953388e-12, 1.2755835948385307e-12, 1.718926371953829e-12, 5.561410412067812e-12, 5.612637550150243e-12, 8.453230126630751e-12, 8.725636132121303e-12, 1.613513316811227e-11, 1.9752204589154072e-11, 3.992415416880054e-11, 5.064793173473998e-11, 6.907684718699313e-11, 7.573645788325202e-11, 1.9359271945838294e-10, 2.5869603101103545e-10, 3.2604244542604767e-10, 6.107383625965949e-10, 3.054349088416707e-09, 3.948608349010387e-09, 4.447405216487758e-09, 1.0212339772102215e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.7445705189856901e-06, 2.288475481497035e-06, 2.4871339408837677e-06, 4.784661357565223e-06, 7.735179302262651e-06, 7.735179302262651e-06, 7.735179302262651e-06, 1.143879077437328e-05, 1.7526502766236366e-05, 2.1197709067643072e-05, 2.6430911436535815e-05, 2.657094056627476e-05, 2.887131448340355e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.4863008760479634e-05, 3.492397816990208e-05, 7.053323604609976e-05, 8.177663548884549e-05, 0.00010292414518908584, 0.00011010458218851394, 0.0001394823618647422, 0.00019911692003617067, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00036828214026394526, 0.00040585244638543746, 0.00044624278933638414, 0.000624263866548228, 0.0006433262237973916, 0.0007951156865591151, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0014101558138724423, 0.0014101558138724423, 0.0016218187650366893, 0.0018435395778915382, 0.00222143411631358, 0.002322745213235068, 0.002322745213235068, 0.0028678013067590753, 0.0028678013067590753, 0.00311777897086883, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0036513779740440966, 0.0036513779740440966, 0.0036513779740440966, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.00475142897834944, 0.00475142897834944, 0.005193932513124365, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.007409574659909205, 0.009752895879302663, 0.009752895879302663, 0.009752895879302663, 0.01039795547744385, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011352761081333766, 0.01220266897077021, 0.01220266897077021, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01329267875817111, 0.014505355414192591, 0.014505355414192591, 0.014505355414192591, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.018660404569681696, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.02318941202441517, 0.02318941202441517, 0.024206613952731663, 0.030469418869701087, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.03421567685770522, 0.044982539971228225, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.04891569108043106, 0.04891569108043106, 0.049897804257083, 0.049897804257083, 0.05467242947627652, 0.05467242947627652, 0.05481710086367935, 0.055089155307102226, 0.055089155307102226, 0.05509817970492246, 0.05677377998146615, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06649111613211628, 0.08749780016064151, 0.08749780016064151, nan, 9.281139595433629e-14, 2.269349834260423e-13, 2.7902576028316086e-13, 4.218835214235241e-13, 4.5077086681763955e-13, 4.82464670143429e-13, 4.842603006971216e-13, 5.193479028606108e-13, 6.068084596209685e-13, 6.610030519110495e-13, 7.212479024605606e-13, 8.568857509989623e-13, 1.6272513763945853e-12, 1.9674709158152676e-12, 2.1372581686426213e-12, 2.254528174258105e-12, 2.2876757671708803e-12, 2.4025583918652713e-12, 2.4241116067787184e-12, 3.0036146073251357e-12, 3.114057776360651e-12, 3.190714414399256e-12, 3.5097864963972432e-12, 3.672330803019696e-12, 3.7398481401433984e-12, 3.990818765183174e-12, 4.365123487983834e-12, 4.8888554308448594e-12, 5.387012900499014e-12, 7.96843236064306e-12, 8.077400370649127e-12, 1.1251193515434908e-11, 1.3675744745562445e-11, 1.3920614660091849e-11, 1.4087308217430403e-11, 1.4273596897235296e-11, 1.461920440928664e-11, 1.5634002646418687e-11, 2.3968977128922425e-11, 2.433923659511027e-11, 2.660223144487514e-11, 2.6664588260367057e-11, 2.676534530323327e-11, 2.8357052592197995e-11, 3.5482465948075545e-11, 3.912823701897628e-11, 4.5792515620344005e-11, 4.83302598018717e-11, 5.7496911875158286e-11, 7.393414940211248e-11, 7.537810324630735e-11, 1.0467186667268884e-10, 1.1561730928248412e-10, 1.2311221790850263e-10, 1.4904172713013177e-10, 1.9074109748783146e-10, 2.0111746088624902e-10, 2.2283480985401337e-10, 2.357187317491253e-10, 2.843497025582179e-10, 2.9354396217842974e-10, 3.776793074685755e-10, 3.9930283126020015e-10, 4.873977459556436e-10, 6.136966626402195e-10, 7.918346129110473e-10, 8.089460836160799e-10, 8.434394278175816e-10, 1.232965109215602e-09, 1.5142285361893607e-09, 1.6328557314948404e-09, 2.035771709232225e-09, 2.2619806140213693e-09, 2.6289372753845552e-09, 3.0025433124197725e-09, 3.035021500823481e-09, 3.206258341498779e-09, 3.3000409154642987e-09, 8.413437663248949e-09, 9.618715278570361e-09, 1.1016869472694786e-08, 1.900790439141643e-08, 2.311826318521997e-08, 3.256531210029739e-08, 7.062069115372226e-08, 1.0379292719811166e-07, 3.5715917986665096e-07, 3.6750233373760955e-07, 6.843164520980078e-07, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3008598108779382e-06, 1.3460252150945272e-06, 1.8929692383974172e-06, 7.735179302262651e-06, 7.735179302262651e-06, 7.735179302262651e-06, 7.735179302262651e-06, 7.735179302262651e-06, 7.735179302262651e-06, 7.735179302262651e-06, 7.735179302262651e-06, 7.735179302262651e-06, 8.411728545722206e-06, 8.778941800484344e-06, 8.942313844705377e-06, 1.4014148371009287e-05, 2.2926951485335274e-05, 2.2926951485335274e-05, 2.431593662720157e-05, 2.4932490260874857e-05, 2.7271518164709887e-05, 2.7313174993637146e-05, 2.9232793694637693e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.0230001662261698e-05, 3.4278826533092836e-05, 3.4360528895444655e-05, 3.5152107842660994e-05, 3.5152107842660994e-05, 3.8734026594771645e-05, 4.565502139540429e-05, 4.597203836887115e-05, 4.822546291257643e-05, 4.8425264344652075e-05, 4.855200308535534e-05, 5.510805469377382e-05, 5.7112188449031483e-05, 6.092159391178477e-05, 6.879571179956485e-05, 8.313809937273007e-05, 0.00011371822284591567, 0.00012011351199907829, 0.0001335844793605124, 0.00013784572395372847, 0.0001394823618647422, 0.0001394823618647422, 0.0001394823618647422, 0.0001394823618647422, 0.0001394823618647422, 0.0001394823618647422, 0.0001394823618647422, 0.0001394823618647422, 0.00015987871167652716, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00021400555532215588, 0.00024385105098149426, 0.0002729346730477522, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.00030495456056691696, 0.0003550239242961073, 0.0003765027072067583, 0.0004240672096162282, 0.0004311923920911356, 0.0004392093202240639, 0.00046548915927821635, 0.00046667500410430354, 0.0004947685893786727, 0.0006369455430736078, 0.000838680562258869, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0011355323237735688, 0.0014101558138724423, 0.0014101558138724423, 0.0014101558138724423, 0.0014101558138724423, 0.0015552826431019483, 0.0016218187650366893, 0.0016218187650366893, 0.0016218187650366893, 0.0016218187650366893, 0.0018435395778915382, 0.0018435395778915382, 0.0018435395778915382, 0.0019042110442508626, 0.00222143411631358, 0.00222143411631358, 0.00222143411631358, 0.00222143411631358, 0.00222143411631358, 0.00222143411631358, 0.00222143411631358, 0.00222143411631358, 0.00222143411631358, 0.0022432887035898808, 0.002322745213235068, 0.002322745213235068, 0.002322745213235068, 0.002322745213235068, 0.002322745213235068, 0.002322745213235068, 0.002322745213235068, 0.002322745213235068, 0.002322745213235068, 0.002322745213235068, 0.0027235004253453373, 0.0028678013067590753, 0.0028678013067590753, 0.0028678013067590753, 0.0028678013067590753, 0.00311777897086883, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.0033861158375087397, 0.003464625013107984, 0.003464625013107984, 0.003464625013107984, 0.0036048379414816956, 0.0036048379414816956, 0.0036513779740440966, 0.0036513779740440966, 0.0036513779740440966, 0.0036513779740440966, 0.003673258086633926, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.003677317310588113, 0.004419966866807584, 0.00475142897834944, 0.00475142897834944, 0.00475142897834944, 0.00475142897834944, 0.00475142897834944, 0.00475142897834944, 0.00475142897834944, 0.00475142897834944, 0.00475142897834944, 0.00475142897834944, 0.004779170485667904, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005337293972714926, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.005377687077724396, 0.008735137230944151, 0.009242277068721074, 0.009242277068721074, 0.009242277068721074, 0.009242277068721074, 0.009242277068721074, 0.009752895879302663, 0.009752895879302663, 0.009752895879302663, 0.009752895879302663, 0.009752895879302663, 0.009752895879302663, 0.01039795547744385, 0.010628779208547554, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.011221522124806933, 0.01220266897077021, 0.01220266897077021, 0.01220266897077021, 0.01220266897077021, 0.01220266897077021, 0.01220266897077021, 0.01220266897077021, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.01250688334305161, 0.014505355414192591, 0.014505355414192591, 0.014505355414192591, 0.014505355414192591, 0.014505355414192591, 0.014505355414192591, 0.014505355414192591, 0.014505355414192591, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.01524607053576118, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015404915329058346, 0.015837213855906463, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.017643610569380134, 0.018660404569681696, 0.018660404569681696, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.020872950531874518, 0.02288815659018242, 0.02288815659018242, 0.02288815659018242, 0.02288815659018242, 0.02288815659018242, 0.02288815659018242, 0.02288815659018242, 0.02288815659018242, 0.02318941202441517, 0.02318941202441517, 0.02318941202441517, 0.02318941202441517, 0.02318941202441517, 0.025601966894170384, 0.02745793240099951, 0.02745793240099951, 0.030469418869701087, 0.030469418869701087, 0.030469418869701087, 0.030469418869701087, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.031649468600811484, 0.03318429749581211, 0.034209255258829244, 0.046861045474881166, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.048298592091885494, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04891569108043106, 0.04981588650755867, 0.049897804257083, 0.049897804257083, 0.049897804257083, 0.049897804257083, 0.049897804257083, 0.049897804257083, 0.049897804257083, 0.049897804257083, 0.049897804257083, 0.05081690247708189, 0.05467242947627652, 0.05467242947627652, 0.05467242947627652, 0.05467242947627652, 0.05467242947627652, 0.05467242947627652, 0.05481710086367935, 0.05481710086367935, 0.05481710086367935, 0.05485546341750821, 0.05485546341750821, 0.05485546341750821, 0.055089155307102226, 0.055089155307102226, 0.055089155307102226, 0.055089155307102226, 0.055089155307102226, 0.055089155307102226, 0.05509817970492246, 0.05509817970492246, 0.05509817970492246, 0.05509817970492246, 0.05509817970492246, 0.05509817970492246, 0.05509817970492246, 0.05509817970492246, 0.05677377998146615, 0.05677377998146615, 0.05677377998146615, 0.05677377998146615, 0.05677377998146615, 0.05677377998146615, 0.05677377998146615, 0.05677377998146615, 0.05703536944842313, 0.05703536944842313, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06442952433704384, 0.06649111613211628, 0.0688743619567419, 0.0688743619567419, 0.0688743619567419, 0.07067059510945926, 0.07067059510945926, 0.07067059510945926, 0.07067059510945926, 0.07067059510945926, 0.07067059510945926, 0.07786785923711323, 0.07786785923711323, 0.07786785923711323, 0.07786785923711323, 0.07786785923711323, 0.07786785923711323, 0.07786785923711323, 0.07917009879824155, 0.08693985693561855, 0.08693985693561855, 0.08696214430364411, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.08749780016064151, 0.09127803768251525, 0.09127803768251525, 0.09638672921682619, 0.09769320936724614, 0.09769320936724614, 0.09769320936724614, 0.10557328883038568, 0.10774376329496757, 0.10774376329496757, 0.10774376329496757, 0.1084390408167554, 0.11822729528682707, 0.11822729528682707, 0.11822729528682707, 0.13535933255150795, 0.1383609827438404, 0.1383609827438404, 0.1383609827438404, 0.1383609827438404, 0.15114253416458057, 0.15853753519355132, 0.15853753519355132, 0.15853753519355132, 0.15853753519355132, 0.15853753519355132, 0.17588077798687632, 0.18864310137300858, 0.18864310137300858, 0.18864310137300858, 0.4043949148776759, 0.4685147721706657, 0.49579460910357864, 0.7469712201476048, 43.95161038164527]

重现错误的代码,但错误相似,其分辨率也可以解决我的初始问题(可能):

test = json.load(open("github_gist.json"))
test = sorted(test)
print(len([item for item in test if item < test[250]]))  # 8

1 个答案:

答案 0 :(得分:4)

您的数据包含nan(“不是数字”)。因为nanFalse之类的所有内容进行比较,这会让人感到困扰。演示:

>>> sorted([0.3, float('nan'), 0.1])
[0.3, nan, 0.1]

要清楚我对比较的意思:

>>> nan = float('nan')
>>> nan < 3
False
>>> nan > 3
False
>>> nan == 3
False
>>> nan == nan
False                 # yeah