Python.sort()不能对数字进行完全排序

Python.sort()不能对数字进行完全排序,python,list,sorting,dictionary,Python,List,Sorting,Dictionary,当从API请求数据时,我会得到一个带有字典的列表,但当我请求对其进行排序时,它不会对所有数字进行完全排序 我运行的代码: def getVolume(e): return e["priceChangePercent"] tickers.sort(reverse=True,key=getVolume) for x in tickers: print(x["symbol"]+" "+x["priceChangePercent"]) 我得到的结果是: DLTBNB 9.729 RL

当从API请求数据时,我会得到一个带有字典的列表,但当我请求对其进行排序时,它不会对所有数字进行完全排序

我运行的代码:

def getVolume(e):
    return e["priceChangePercent"]

tickers.sort(reverse=True,key=getVolume)
for x in tickers:
    print(x["symbol"]+" "+x["priceChangePercent"])
我得到的结果是:

DLTBNB 9.729
RLCBNB 9.327
BRDBNB 9.087
EVXETH 8.699
||更多正确排序的数字| | DASHBNB 3.123
阿德里思3.093
MATICBNB 23.832
MATICUSDT 20.087
XMRETH 2.946
||随机出现23个不属于那里的||
BCHSVUSDC 0
NXSBTC-9.700
GASBTC-9.585
SKYBTC-9.357
请求tickers列表中dic数据时的字典示例:

{'symbol': 'ETHBTC', 'priceChange': '-0.00121400', 'priceChangePercent': '-4.363', 'weightedAvgPrice': '0.02695265', 'prevClosePrice': '0.02782400', 'lastPrice': '0.02661100', 'lastQty': '3.29400000', 'bidPrice': '0.02661100', 'bidQty': '6.70600000', 'askPrice': '0.02661900', 'askQty': '19.09500000', 'openPrice': '0.02782500', 'highPrice': '0.02841700', 'lowPrice': '0.02616300', 'volume': '320008.07300000', 'quoteVolume': '8625.06693308', 'openTime': 1557612831863, 'closeTime': 1557699231863, 'firstId': 121467734, 'lastId': 121655972, 'count': 188239}

您正在对字符串而不是数字进行排序,您的
getVolume
需要首先将值转换为
float

def getVolume(e):
    return float(e["priceChangePercent"])
或者,如果希望
getVolume
返回字符串,可以在
函数中进行转换:

tickers.sort(reverse = True, key = lambda x : float(getVolume(x)))

谢谢你,先生,你救了我一天。不客气@Python_Noob,如果这个答案有帮助,你可以将其标记为接受。