我想对这个数组进行排序,但输出是错误的。它排序不会随着“False”而增加。
更改号码后即可使用
有人可以帮我解释一下吗?
prices = [{ 'price': '9.87940000'}, { 'price': '9.94400000'}, {'price': '10.33820000'}, {'price': '10.73820000'}]
prices_sorted = sorted(prices, key=lambda x: x['price'], reverse=False)
for prices in prices_sorted :
print(prices)
The output :
{'price': '10.33820000'}
{'price': '10.73820000'}
{'price': '9.87940000'}
{'price': '9.94400000'}
Two wrongs : 10.33820000 < 10.73820000 and not increasing with "False"
by anpther number it works
prices = [{ 'price': '5.87940000'}, { 'price': '6.94400000'}, {'price': '7.33820000'}, {'price': '8.73820000'}]
prices_sorted = sorted(prices, key=lambda x: x['price'], reverse=False)
for prices in prices_sorted :
print(prices)
The output :
{'price': '5.87940000'}
{'price': '6.94400000'}
{'price': '7.33820000'}
{'price': '8.73820000'}
您遇到的问题是由于在排序时价格被视为字符串而不是数字。
价格 = [{'价格': 9.87940000}, {'价格': 9.94400000}, {'价格': 10.33820000}, {'价格': 10.73820000}]
----根据“价格”键对字典列表进行排序 prices_sorted = 排序(价格,key=lambda x:x['price'],reverse=False)
----打印字典的排序列表 对于prices_sorted中的价格: 打印(价格)