将带有浮点数和整数的字典列表排序为特定顺序
sort a list of dict's with floats and int's into specific order
我有一个字典的列表,例如
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 但我不知道如何更改它以使其工作任何帮助将不胜感激 FYI 从 routes.txt 文件导入的字典列表因此一些数字浮动,一些 int 对如何使这项工作有任何想法?或者我对代码做错了什么
您可以使用自定义 key=
函数,将字符串转换为 float
和 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},
]
我有一个字典的列表,例如
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 但我不知道如何更改它以使其工作任何帮助将不胜感激 FYI 从 routes.txt 文件导入的字典列表因此一些数字浮动,一些 int 对如何使这项工作有任何想法?或者我对代码做错了什么
您可以使用自定义 key=
函数,将字符串转换为 float
和 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},
]