Printing a sorted list doesn't work fully, how can I fix this issue?

Code that I’ve written:

opener = open("gymclub.txt", "r") reader = opener.readline() listObjects = [] listNames = [] listPressups = [] listPullups = [] sortedListPressups = [] while reader!="":     splitting=reader.split(",")     name = splitting[0]     press_ups = splitting[1]     pull_ups = splitting[2]     reader = opener.readline()     listPressups.append(press_ups)     sortedListPressups = sorted(listPressups, reverse = True) print(sortedListPressups) 

Outcome:

['8', '75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10'] 

Desired Outcome:

['75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10','8'] 

What have I done wrong?

Edit: This is the text file:

enter image description here

Add Comment
4 Answer(s)

The sorting is currently done lexicographically since the elements are strings. To have them formatted as per numbers, you have to convert them into numbers. You can do it as follows for any list

arr = ['8', '75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10'] print(sorted(list(map(int,arr)),reverse=True))  

OUTPUT:

[75, 74, 73, 67, 66, 58, 45, 33, 30, 25, 10, 8] 

So, in your case, you could do it as follows

opener = open("gymclub.txt", "r") reader = opener.readline() listObjects = [] listNames = [] listPressups = [] listPullups = [] sortedListPressups = [] while reader!="":     splitting=reader.split(",")     name = splitting[0]     press_ups = splitting[1]     pull_ups = splitting[2]     reader = opener.readline()     listPressups.append(press_ups)     sortedListPressups = (sorted(list(map(int,listPressups)),reverse=True)) print(sortedListPressups) 

OUTPUT:

[75, 74, 73, 67, 66, 58, 45, 33, 30, 25, 10, 8] 

If you want the final list to be a list of strings itself as it originally was, then you could also do it in the following manner

opener = open("gymclub.txt", "r") reader = opener.readline() listObjects = [] listNames = [] listPressups = [] listPullups = [] sortedListPressups = [] while reader!="":     splitting=reader.split(",")     name = splitting[0]     press_ups = splitting[1]     pull_ups = splitting[2]     reader = opener.readline()     listPressups.append(press_ups)     sortedListPressups = sorted(listPressups, reverse = True, key = int) print(sortedListPressups) 

OUTPUT:

['75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10','8'] 

You could also do sortedListPressups.sort(reverse = True, key = int) instead of using sorted() if you are going to sort in place . You can read more about these methods here

Add Comment

Sorting characters leads to this outcome. You need to cast string elements to numbers:

opener = open("gymclub.txt", "r") reader = opener.readline() listObjects = [] listNames = [] listPressups = [] listPullups = [] sortedListPressups = [] while reader!="":     splitting=reader.split(",")     name = splitting[0]     press_ups = int(splitting[1])     pull_ups = splitting[2]     reader = opener.readline()     listPressups.append(press_ups)     sortedListPressups = sorted(listPressups, reverse = True) print(sortedListPressups) 
Add Comment

You need to convert the lines your read from strings to integers. Right now sorted sorts the strings in the list in lexicographic order.

opener = open("gymclub.txt", "r") reader = opener.readline() listObjects = [] listNames = [] listPressups = [] listPullups = [] sortedListPressups = [] while reader!="":     splitting=reader.split(",")     name = splitting[0]     press_ups = splitting[1]     pull_ups = splitting[2]     reader = opener.readline()     listPressups.append(int(press_ups))  listPressups.sort(reverse=True) print(listPressups) 
  1. I’ve taken the sort out of the loop for you. There is no need to sort the whole list every single iteration.
  2. I’ve also used list.sort() instead of sorted to sort the list in place.

If you do want to preserve the behavior in which the items in the list are strings, you can use the key parameter and provide it with a function to sort by:

listPressups.sort(key=int, reverse=True) 
Add Comment

You’re sorting strings, not numbers.

>>> '8' > '75' True 

If you want to sort these elements according to their integer value, you could pass the key argument when sorting. This keeps the original array unchanged.

>>> sorted(listPressups, key=int, reverse=True) ['75', '74', '73', '67', '66', '58', '45', '33', '30', '25', '10', '8'] 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.