Peque
Peque

Reputation: 14811

Python: How to order a list of float

Having a list of floats:

a = [465.12, 405.85714285714278, 407.4285714285715, 408.0, 408.1874999999996, 409.875, 411.0, 411.75000000000063, 413.43749999999972, 414.0, 414.66666666666652, 416.33333333333201, 418.0, 417.33333333333252, 419.666666666666, 420.0, 420.74999999999966, 422.70000000000067, 423.0, 423.35714285714295, 425.49999999999994, 426.0, 426.37500000000011, 428.02499999999992, 429.0, 430.03125000000006, 431.8125, 432.0, 432.1874999999996, 434.0625000000004, 435.0, 435.32432432432432, 437.43243243243262, 438.0, 438.39999999999992, 440.40000000000009, 441.0, 441.32432432432427, 442.9459459459456, 444.0, 444.42857142857122, 445.97142857142865, 447.0, 447.92857142857116, 450.0, 450.0, 450.60000000000002, 452.72727272727263, 453.0, 453.19148936170194, 454.78723404255322, 456.0, 456.5, 458.5, 459.0, 459.44999999999959, 461.40000000000015, 462.0, 462.50000000000023, 464.16666666666725]

I am trying to sort that list by descendant values (notice I need ind because later I will be sorting other lists, such as sorted_b, with it):

ind = sorted(range(len(a)), key = lambda i:a[i], reverse = True)

sorted_a = [x for (i, x) in sorted(zip(ind, a))]

# sorted_b = [x for (i, x) in sorted(zip(ind, b))]

But the code does not work as I would expect:

>>> sorted_a
[465.12, 464.16666666666725, 462.5000000000002, 462.0, 461.40000000000015, 459.4499999999996, 459.0, 458.5, 456.5, 456.0, 454.7872340425532, 453.19148936170194, 452.72727272727263, 453.0, 450.6, 450.0, 450.0, 447.92857142857116, 447.0, 445.97142857142865, 444.4285714285712, 444.0, 442.9459459459456, 441.32432432432427, 441.0, 440.4000000000001, 438.3999999999999, 438.0, 437.4324324324326, 435.3243243243243, 435.0, 434.0625000000004, 432.1874999999996, 432.0, 431.8125, 430.03125000000006, 429.0, 428.0249999999999, 426.3750000000001, 426.0, 425.49999999999994, 423.35714285714295, 423.0, 422.70000000000067, 420.0, 420.74999999999966, 419.666666666666, 417.3333333333325, 418.0, 416.333333333332, 414.6666666666665, 414.0, 413.4374999999997, 411.7500000000006, 411.0, 409.875, 408.1874999999996, 408.0, 407.4285714285715, 405.8571428571428]

Some values are not correctly sorted as we can easily see in the figure:

import matplotlib.pyplot as mpl
mpl.plot(sorted_a)
mpl.show()

Bad sorting

What am I doing wrong?

Upvotes: 1

Views: 248

Answers (2)

Fred Foo
Fred Foo

Reputation: 363627

sorted(zip(ind, a))

does a lexicographic sort on the (index, value) pairs coming from zip; since the integer indices are all unique by construction, the sort never even looks at the values and just sorts the indices. What you meant is just

[a[i] for i in ind]

or better, if NumPy is an option:

a = np.array(a)
ind = np.argsort(a)
sorted_a = a[ind]

Upvotes: 3

DrV
DrV

Reputation: 23510

First you form the sort order correctly:

ind = sorted(range(len(a)), key = lambda i:a[i], reverse = True)

But then if you want to have sorted a, you get it as easily as:

[ a[i] for i in ind ]

Maybe that helps?

Upvotes: 2

Related Questions