0

I have been trying to sort a list of elements (string) according to their occurrence in Python3. I have been using the inbuilt sort() method with the string.count as key as shown below.

p = "acaabbcabac"
print(sorted(p, key=p.count))
# Output : ['c', 'b', 'b', 'c', 'b', 'c', 'a', 'a', 'a', 'a', 'a']
#But expected output is ['a','a','a','a','a','b','b','b','c','c','c']

p = "acaabbcb"
print(sorted(p, key=p.count))
# Output : ['c', 'c', 'a', 'a', 'a', 'b', 'b', 'b']
#Output is as expected 

p = "ababab"
print(sorted(p, key=p.count))
# Output :['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'] 
#But expected output is ['a','a','a','b','b','b']

What I have observed is, the above sort works as per the occurrence of the element, but it works only if the counts of each element is different. If the occurrence of any two or more elements is same, then they are listed in the same order they appear in the string/list.

Am I doing something wrong or is there a better approach at this ? I tried searching answers for this issue but I could not find and so am posting this here.

Thanks in advance.

M.javid
  • 6,387
  • 3
  • 41
  • 56
user3543477
  • 615
  • 3
  • 13
  • 26
  • That's because `sorted` sorts your list in a ascending order. You can use `reverse=True` keyword arg to make it descending – Mazdak Oct 07 '18 at 08:05
  • @Kasrâmvd: please try the example 3, the output would be same as the input, the letters would not even be next to each other in the output. – user3543477 Oct 07 '18 at 08:16

1 Answers1

0

Use a lambda function in your sorting key, where the first operation is p.count, and the second simply sorts on the element value (which ends up being alphabetical):

p = "ababab"
sorted(p, key = lambda x: [p.count, x])
# ['a', 'a', 'a', 'b', 'b', 'b']
andrew_reece
  • 20,390
  • 3
  • 33
  • 58
  • Why doesn't the in built sort/sorted method work this way? I believe it is supposed to do the same thing, but it gives up if count of two chars is the same, I wonder why. – user3543477 Oct 07 '18 at 08:18
  • I would like to sort it with the highest occurence at the start or end of the output – user3543477 Oct 07 '18 at 08:23