My mission is to sort the list by the frequency of numbers included in it. If a few numbers have an equal frequency - they should be sorted according to their natural order.
For example: [5, 2, 4, 1, 1, 1, 3] ==> [1, 1, 1, 2, 3, 4, 5]
During the process of solving the problem, I wrote a function as follow:
lista = [3, 4, 11, 13, 11, 4, 4, 7, 3]
def func1(numbers: list):
numbers.sort(key=lambda x:(-numbers.count(x), x))
return numbers
result = func1(lista)
print(result)
but the result is [3, 3, 4, 4, 4, 7, 11, 11, 13],
and then I wrote a function very similar to that
lista = [3, 4, 11, 13, 11, 4, 4, 7, 3]
def func2(numbers: list):
return sorted(numbers,key=lambda x:(-numbers.count(x), x))
result = func2(lista)
print(result)
the result is [4, 4, 4, 3, 3, 11, 11, 7, 13], which is what I wanted
My question is: what's different between list.sort function and sorted function
it is just a few part of my whole homework, please ignore the time complexity of my algorithm.