How do I place items on a list in ascending or descending order?

Asked

Viewed 36,794 times

-1

I am trying to put the numbers of a list in ascending order but the Sort() method is putting the 10 and its multiples (20, 30) as being similar to itself divided by 10. For example, in the following code:

numeros = ['1', '3', '2', '20', '4', '10', '30']  
numeros.sort()  
print(numeros)  

He returns me as an answer:

['1', '10', '2', '20', '3', '30', '4']  

Instead of:

['1', '2', '3', '4', '10', '20', '30']  

As expected. So why does this happen and how do I get them to grow? And how do I put them in descending order?

  • 1

    You are ordering strings, not numbers, and strings are ordered alphabetically. I’m on my cell phone and I can’t see if I’ve ever duplicated this question, but the answers would be sort(numeros, key=int) and sort(numeros, key=int, reversed=True).

  • It worked, thanks, but only actually when I put: .Sort(key=int) numbers or.Sort(key=int, Reverse=True . But you responded in the form of a comment, then you put it in response even so I can signal that you helped and stuff...

  • @Andersoncarloswoss I could not find a duplicate of this question, although I have the feeling that I have seen it. So I think the answer would be useful.

1 answer

4


Python is a dynamic language, meaning that there is no limitation of the type a function can take as argument. The function can receive a string, now can receive an integer and, with this, it is customary to make the function can respond in an expected way to all. An implication of this, when especially analyzed the function sorted, is that when you pass a list of numbers, they will be ordered increasing but when passed a list of strings, will be sorted alphabetically. You have a list of strings, then the output will be alphabetical, not numerical, and thus there are two considerations to be made, which by the way are unique to each other - that is, one or the other:

  1. You have a list of strings that have numeric characters; you need them to remain strings, because of the semantics of your problem this will make sense and just want to order them numerically.

If this is your situation, then you should use the argument key of function sorted. Through it you will indicate a calling object - it can be a function, a class, an object that implements __call__, etc - and thus the function will use it when analyzing the list values before sorting them. By default, this argument is None and the list is ordered by directly considering the values; when different from None, the list is ordered considering the return of the searchable object passed by key for each value in the list. This object will always be memoised - applied memoization - which means that the function will be called only once for each value of the list, which does not affect both the performance.

How do you have a list of strings and wants to hate them as whole, just pass the class int through the argument key:

>>> sorted(numeros, key=int)
['1', '2', '3', '4', '10', '20', '30']

Thus, the list will be ordered according to the return of int for each value, but the result will remain as a list of strings. If you want to order them from form,a reversa, just indormar the argument reverse as true:

>>> sorted(numeros, key=int, reverse=true)
['30', '20', '10', '4', '3', '2', '1']

However, if after sorting the list you no longer need to work with the original list, prefer to use the method sort instead of the function sorted:

numeros.sort(key=int)  # Crescente
numeros.sort(key=int, reverse=True)  # Decrescente

Because the method will only modify the list itself in memory, while the function sorted will generate a new list, consuming twice the memory of the application.

  1. You have a list of strings which have numeric characters; it makes no sense that they are strings and semantically the values should be integers - imagine a list of ages: age is an integer value, not an string;

For this situation, the recommended thing to do is to convert the values to integers and only then sort them as you wish. For this you will necessarily have to create a new list, so there are no preferences between sorted or sort. To avoid unnecessary memory expenditure, you can create a generator to make the strings for integers and order the generator itself. The generator is has lazy execution, that is, it only executes its logic when necessary, so you will not have to create a list of integers not ordered before ordering them - although create the temporary list and sort it through the method sort will have the same result; use the one that is most convenient.

To create the list of strings in integer list, just use the function map:

numeros_inteiros = map(int, numeros)

And thus order them in increasing or decreasing form:

>>> sorted(numeros_inteiros)  # Crescente
[1, 2, 3, 4, 10, 20, 30]

>>> sorted(numeros_inteiros, reverse=True)  # Decrescente
[30, 20, 10, 4, 3, 2, 1]

If you prefer to create the temporary list and use the method sort, would look something like:

numeros = [int(numero) for numero in numeros]
numeros.sort()  # Crescente
numeros.sort(reverse=True)  # Decrescente

Note that, as I commented in both situations, in the first the result remains a list of strings, but sorted numerically, while in the second the result will already be a list of integers.

  • I tried to apply my list [0, 2, 4, 6, 8, 10, 0, 3, 6, 9, 12, 15] and it didn’t work: list.Sort(key=int) came out [0, 2, 4, 6, 8, 10, 0, 3, 6, 9, 12, 15]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.