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:
- 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.
- 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.
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)
andsort(numeros, key=int, reversed=True)
.– Woss
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...
– Iago Tito
@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.
– Isac