I want to filter the following data:
[{'param_2': '50'}, {'param_2': '10'} ,{'param_2': '100'}, {'param_2': '0.7'}, {'param_2': '1.8'}, {'param_2': '0.25'}, {'param_2': '0. 18'}, {'param_2': '0.63'}, {'param_2': '1'}, {'param_2': '1.6-2.8'}, {'param_2': '1.6-2.1'},{'param_2': '1000'},{'param_2': '500'}]
I want to sort it in ascending order with:
sorted()
I have tried
param_2_filter = sorted(data, key=itemgetter('param_2'))
output:
0.18
0.25
0.63
0.7
1
1.6-2.1
1.6-2.8
1.8
10
100
1000
50
500
can someone please help?
Thanks.
>Solution :
You could do the following which implements the logic of your sort:
key = lambda d: [float(x.replace(" ", "")) for x in d["param_2"].split("-", 1)]
param_2_filter = sorted(data, key=key)
Split on any dash and convert the bits into floats.