字典双重排序

来源:互联网 发布:淘宝a店是什么意思 编辑:程序博客网 时间:2024/05/12 17:28
''''----python3----Find the count of characters in a string and sort them according to the count, if the alphabeticals have the same count, output them in a alphabetical order.'''def sortString(s):    dic = {}    sortedlist = []    keylist = []    for i in range(len(s)):        dic[s[i]] = dic.get(s[i],0)+1    sortedlist = sorted(dic.items(),key=lambda t:(-t[1],t[0]),reverse = False)    #print(sortedlist)    for key in sortedlist:        keylist.append(key[0])    keylist = ','.join(keylist)    print (keylist)    sortString('dccccbadcbaaaaaaddcbadde')
0 0
原创粉丝点击