hackerrank Most Common

来源:互联网 发布:淘宝代销不赚钱 编辑:程序博客网 时间:2024/05/22 11:31

题目:https://www.hackerrank.com/challenges/most-commons/problem
题意:按照次数升序,次数相同降序
思路:s = sorted(Counter(input()).items(), key= lambda x: (-x[1],x[0]))[:3]
详细可见python dict sorted 排序
代码:

'''-*- coding: utf-8 -*-@Author  : PlayerGuan@Time    : 2017/10/14 23:12@Software: PyCharm Community Edition@File    : main.py'''from collections import Counters = sorted(Counter(input()).items(), key= lambda x: (-x[1],x[0]))[:3]for x in s:    print(x[0]+" "+str(x[1]))