Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sort function in python doesn't work for long list?

a=['0', '0.05', '0.1', '0.15', '0.2', '0.25', '0.3', '0.35', '0.4', '0.45', '0.5', '0.55', '0.6', '0.65', '0.7', '0.75', '0.8', '0.85', '0.9', '0.95', '1', '1.05', '1.1', '1.15', '1.2', '1.25', '1.3', '1.35', '1.4', '1.45', '1.5', '1.55', '1.6', '1.65', '1.7', '1.75', '1.8', '1.85', '1.9', '1.95', '10', '10.05', '10.1', '10.15', '10.2', '10.25', '10.3', '10.35', '10.4', '10.45', '10.5', '10.55', '10.6', '10.65', '10.7', '10.75', '10.8', '10.85', '10.9', '10.95', '11', '11.05', '11.1', '11.15', '11.2', '11.25', '11.3', '11.35', '11.4', '11.45', '11.5', '11.55', '11.6', '11.65', '11.7', '11.75', '11.8', '11.85', '11.9', '11.95', '12', '12.05', '12.1', '12.15', '12.2', '12.25', '12.3', '12.35', '12.4', '12.45', '12.5', '12.55', '12.6', '12.65', '12.7', '12.75', '12.8', '12.85', '12.9', '12.95', '13', '13.05', '13.1', '13.15', '13.2', '13.25', '13.3', '13.35', '13.4', '13.45', '13.5', '13.55', '13.6', '13.65', '13.7', '13.75', '13.8', '13.85', '13.9', '13.95', '14', '14.05', '14.1', '14.15', '14.2', '14.25', '14.3', '14.35', '14.4', '14.45', '14.5', '14.55', '14.6', '14.65', '14.7', '14.75', '14.8', '14.85', '14.9', '14.95', '15', '15.05', '15.1', '15.15', '15.2', '15.25', '15.3', '15.35', '15.4', '15.45', '15.5', '15.55', '15.6', '15.65', '15.7', '15.75', '15.8', '15.85', '15.9', '15.95', '16', '16.05', '16.1', '16.15', '16.2', '16.25', '16.3', '16.35', '16.4', '16.45', '16.5', '16.55', '16.6', '16.65', '16.7', '16.75', '16.8', '16.85', '16.9', '16.95', '17', '17.05', '17.1', '17.15', '17.2', '17.25', '17.3', '17.35', '17.4', '17.45', '17.5', '17.55', '17.6', '17.65', '17.7', '17.75', '17.8', '17.85', '17.9', '17.95', '18', '18.05', '18.1', '18.15', '18.2', '18.25', '18.3', '18.35', '18.4', '18.45', '18.5', '18.55', '18.6', '18.65', '18.7', '18.75', '18.8', '18.85', '18.9', '18.95', '19', '19.05', '19.1', '19.15', '19.2', '19.25', '19.3', '19.35', '19.4', '19.45', '19.5', '19.55', '19.6', '19.65', '19.7', '19.75', '19.8', '19.85', '19.9', '19.95', '2', '2.05', '2.1', '2.15', '2.2', '2.25', '2.3', '2.35', '2.4', '2.45', '2.5', '2.55', '2.6', '2.65', '2.7', '2.75', '2.8', '2.85', '2.9', '2.95', '20', '20.05', '20.1', '20.15', '20.2', '20.25', '20.3', '20.35', '20.4', '20.45', '20.5', '20.55', '20.6', '20.65', '20.7', '20.75', '20.8', '20.85', '20.9', '20.95', '21', '21.05', '21.1', '21.15', '21.2', '21.25', '21.3', '21.35', '21.4', '21.45', '21.5', '21.55', '21.6', '21.65', '21.7', '21.75', '21.8', '21.85', '21.9', '21.95', '22', '22.05', '22.1', '22.15', '22.2', '22.25', '22.3', '22.35', '22.4', '22.45', '22.5', '22.55', '22.6', '22.65', '22.7', '22.75', '22.8', '22.85', '22.9', '22.95', '23', '23.05', '23.1', '23.15', '23.2', '23.25', '23.3', '23.35', '23.4', '23.45', '23.5', '23.55', '23.6', '23.65', '23.7', '23.75', '23.8', '23.85', '23.9', '23.95', '24', '24.05', '24.1', '24.15', '24.2', '24.25', '24.3', '24.35', '24.4', '24.45', '24.5', '24.55', '24.6', '24.65', '24.7', '24.75', '24.8', '24.85', '24.9', '24.95', '25', '25.05', '25.1', '25.15', '25.2', '25.25', '25.3', '25.35', '25.4', '25.45', '25.5']
a.sort()
print (a)

>Solution :

You need to convert your list in a list of float, not of string

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a = [*map(float, a)] #  [*map(float, a)] is equivalent to list(map(float, a))
a.sort()
print(a)

Should work.

If you want to convert it back to str you can do:

a = [*map(str,a)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading