我有一個 dict 的串列,例如
list = [{'route number': '996', 'happy customers': '0'}, {'route number': '123', 'happy customers': '4'}, {'route number': '321', 'happy customers': '0.00'}, {'route number': '456', 'happy customers': '5'}, {'route number': '856', 'happy customers': '8.760'}, {'route number': '555', 'happy customers': '1.1'}, {'route number': '565', 'happy customers': '0'}, {'route number': '784', 'happy customers': '3.010'}]
我正在嘗試按滿意的客戶對它們進行排序,如果滿意的客戶 == 0,則按路線編號排序,我已經嘗試過:
sorted_list = sorted(list, key = operator.itemgetter ("happy customers", "route number"), reverse = True)
輸出是
[{'route number': '856', 'happy customers': '8.760'}, {'route number': '456', 'happy customers': '5'}, {'route number': '123', 'happy customers': '4'}, {'route number': '784', 'happy customers': '3.010'}, {'route number': '555', 'happy customers': '1.1'}, {'route number': '321', 'happy customers': '0.00'}, {'route number': '996', 'happy customers': '0'}, {'route number': '565', 'happy customers': '0'}]
預期輸出:
[{'route number': '856', 'happy customers': '8.760'}, {'route number': '456', 'happy customers': '5'}, {'route number': '123', 'happy customers': '4'}, {'route number': '784', 'happy customers': '3.010'}, {'route number': '555', 'happy customers': '1.1'}, {'route number': '321', 'happy customers': '0.00'}, {'route number': '565', 'happy customers': '0'}, {'route number': '996', 'happy customers': '0'}]
我認為這與
route number': '321', 'happy customers': '0.00'}
最后的數字是一個浮點數 0.00 不僅僅是一個 int 0 但我不知道如何更改它以使其作業關于如何使這項作業?或者我在代碼上做錯了什么
uj5u.com熱心網友回復:
您可以使用自定義key=函式,將字串轉換為floatand int:
lst = [
{"route number": "996", "happy customers": "0"},
{"route number": "123", "happy customers": "4"},
{"route number": "321", "happy customers": "0.00"},
{"route number": "456", "happy customers": "5"},
{"route number": "856", "happy customers": "8.760"},
{"route number": "555", "happy customers": "1.1"},
{"route number": "565", "happy customers": "0"},
{"route number": "784", "happy customers": "3.010"},
]
out = sorted(
lst, key=lambda k: (-float(k["happy customers"]), int(k["route number"]))
)
print(out)
印刷:
[
{"route number": "856", "happy customers": "8.760"},
{"route number": "456", "happy customers": "5"},
{"route number": "123", "happy customers": "4"},
{"route number": "784", "happy customers": "3.010"},
{"route number": "555", "happy customers": "1.1"},
{"route number": "321", "happy customers": "0.00"},
{"route number": "565", "happy customers": "0"},
{"route number": "996", "happy customers": "0"},
]
編輯:要將字串轉換為數字:
lst = [
{
"route number": int(d["route number"]),
"happy customers": float(d["happy customers"]),
}
for d in lst
]
out = sorted(lst, key=lambda k: (-k["happy customers"], k["route number"]))
print(out)
印刷:
[
{"route number": 856, "happy customers": 8.76},
{"route number": 456, "happy customers": 5.0},
{"route number": 123, "happy customers": 4.0},
{"route number": 784, "happy customers": 3.01},
{"route number": 555, "happy customers": 1.1},
{"route number": 321, "happy customers": 0.0},
{"route number": 565, "happy customers": 0.0},
{"route number": 996, "happy customers": 0.0},
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/462158.html
