测试工程师面试之算法题字符统计并按照数量大小排序

时间:2020-07-12 22:08:22   收藏:0   阅读:82

python语言:

 

import re
 
while True:
    try:
        line = raw_input()
        li = re.findall(r‘[a-zA-z0-9 ]‘, line)
        s = ‘‘.join(set(li))
        dic = {}
        for c in s:
            dic[c] = line.count(c)
        dli = sorted(dic.iteritems(), key = lambda c: ord(c[0]))
        dli = sorted(dli, key = lambda c: c[1], reverse = True)
        print ‘‘.join(c[0] for c in dli)
    except:
        break
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!