79090198

Date: 2024-10-15 13:40:14
Score: 2.5
Natty:
Report link

Don't have enough reputation to comment, so posting instead. In Python 3.10.12 doing

from collections import Counter

a = [1, 0, 1, 0]
s = sorted(a, key=Counter(a).get, reverse=True)
print(Counter(a).most_common())
print(a)
print(s)

gives

[(1, 2), (0, 2)]
[1, 0, 1, 0]
[1, 0, 1, 0]

So using sorted/sort might not give the expected results (I would have expected [0,0,1,1] or [1,1,0,0]). You might want to use another method of sorting for this use case. Doing

from collections import Counter

a = [1, 0, 1, 0]
d = [item for items, c in Counter(a).most_common() for item in [items] * c]
print(a)
print(d)

gives

[1, 0, 1, 0]
[1, 1, 0, 0]

You might need to sort again if the ordering of the sorted list is important.

Reasons:
  • Blacklisted phrase (1): to comment
  • RegEx Blacklisted phrase (1.5): Don't have enough reputation to comment
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: m-julian