0

Why doesn't it work?

numbers = [3, 4, 11, 13, 11, 4, 4, 7, 3]
numbers.sort(key=lambda x: numbers.count(x))
print(numbers)

Why is the output still [3, 4, 11, 13, 11, 4, 4, 7, 3]

sorted No such problem

azro
  • 53,056
  • 7
  • 34
  • 70
  • 2
    Also: https://stackoverflow.com/questions/25815377/sort-list-by-frequency – luk2302 May 29 '22 at 09:24
  • `list.sort` does it inplace, so you can't relate on it for sorting so `numbers = sorted(numbers, key=lambda x: numbers.count(x))` works or copy the list `cc = list(numbers) numbers.sort(key=lambda x: cc.count(x))` – azro May 29 '22 at 09:42

0 Answers0