tengo una lista de dict, por ejemplo
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'}]
y estoy tratando de ordenarlos por clientes satisfechos y si los clientes satisfechos == 0, entonces ordene por número de ruta. He intentado esto:
sorted_list = sorted(list, key = operator.itemgetter ("happy customers", "route number"), reverse = True)
la salida es
[{'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'}]
Rendimiento esperado:
[{'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'}]
creo que tiene algo que ver
route number': '321', 'happy customers': '0.00'}
el número al final es un flotante 0.00, no solo un int 0, pero no sé cómo cambiarlo para que funcione. Cualquier ayuda sería apreciada. Para su información, la lista de dict importados de un archivo route.txt, por lo tanto, algunos números flotan y algunos int tienen alguna idea. sobre cómo hacer que esto funcione?? o lo que estoy haciendo mal con el código
Solución del problema
Puede usar la key=
función personalizada, donde convierte las cadenas en float
y 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)
Huellas dactilares:
[
{"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"},
]
EDITAR: Para convertir las cadenas en números:
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)
Huellas dactilares:
[
{"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},
]
No hay comentarios:
Publicar un comentario