i have a list of dict's eg
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'}]
and i am trying to sort them by happy customers and if happy customers == 0 then sort by route number i have tried this:
sorted_list = sorted(list, key = operator.itemgetter ("happy customers", "route number"), reverse = True)
output is
[{'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'}]
expected output:
[{'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'}]
i think it has something to do with
route number': '321', 'happy customers': '0.00'}
number on the end being a float 0.00 not just an int 0 but i dont know how to change it to make it work any help would be appreciated FYI list of dict's imported from a routes.txt file hence some numbers floats and some int's any ideas on how to make this work ?? or what i am doing wrong with the code
CodePudding user response:
You can use custom key=
function, where you convert the strings to float
and 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)
Prints:
[
{"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"},
]
EDIT: To convert the strings to numbers:
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)
Prints:
[
{"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},
]