Sort by

recency

|

1302 Discussions

|

  • + 0 comments
    if __name__ == '__main__':
        s = input()
        
        counts = {}
        
        for i in s:
            counts[i] = s.count(i)
            
        counts = sorted(counts.items(), key=lambda x: (-x[1], x[0]))
        
        for i in range(3):
            print(counts[i][0], counts[i][1])
    
  • + 0 comments
    import math
    import os
    import random
    import re
    import sys
    from collections import Counter
    
    
    if __name__ == '__main__':
        s = input()
    
    count= Counter(s)
    sorted_items = sorted(count.items(), key=lambda x: (-x[1], x[0]))
    for char, freq in sorted_items[:3]:
        print(char, freq)
        
    
    ``
    
  • + 0 comments

    Did a little digging and found the Counter.most_common(n) method, which returns the list you need to iterate print(*) through. Sorted the string beforehand because .most_common(n) will sort by order of elements encountered.

    from collections import Counter
    
    
    if __name__ == '__main__':
        s = sorted(input())
        s_count = Counter(s)
        [print(*entry) for entry in s_count.most_common(3)]
    
  • + 0 comments
    from collections import Counter
    
    
    if __name__ == '__main__':
        s = input()
        c = Counter(s)
        sort_val = sorted(c.values(), reverse=True) #nos. sorted in ascending order
        sort_counter = dict(sorted(c.items())) #sorted the counter
        i = 0
        while(i<3):
            for k,v in sort_counter.items():
                if v==sort_val[i]:
                    print(k +" "+ str(v))
                    sort_counter.pop(k)
                    i+=1
                    break
    
  • + 0 comments

    I did this:

    s = input().lower()
    
    # Create dictionary of each char count
    dict = {char: s.count(char) for char in s}
    
    # Return list of tuples by value count in descending order, and alphabetic for equal values
    sorted_list = sorted(dict.items(), key = lambda x: (-x[1], x[0]))
    
    # Print top three values of sorted_list
    for key, value in sorted_list[:3]:
        print(key, value)