HJ102 字符统计

描述
输入一个只包含小写英文字母和数字的字符串,按照不同字符统计个数由多到少输出统计结果,如果统计的个数相同,则按照ASCII码由小到大排序输出。
本题含有多组样例输入

输入描述:
一个只包含小写英文字母和数字的字符串。

输出描述:
一个字符串,为不同字母出现次数的降序表示。若出现次数相同,则按ASCII码的升序输出

while True:
    try:
        l = input()
        zimu = []
        cishu = []
        for i in l:
            if i not in zimu:
                zimu.append(i)
                cishu.append(l.count(i))
        result = list(zip(zimu,cishu)) #将字母和出现次数打包成一个元组存入数组
        #print(result)
        newresult = sorted(result,key=lambda x:x[0] ) #将存了元组的数组按字母升序排序
        #print(newresult)

        newresult = sorted(newresult,key=lambda x:x[1] ,reverse=True) #再按出现次数降序排序
        #for i,j in newresult:
        newresult = list(zip(*newresult))[0] #将newresult解包,变为[(zimu),(cishu)],存下zimu
        print(''.join(newresult))
        
        ###sorted(list,key=lambda x:x*2) 这里key参数是指将list的元素当成x参数,x*2是需要执行的表达式,将表达式的值赋给key
        ###然后按key的值给list排序
        ###下面的key=lambda x:s.count(x)*1000-ord(x),指将去重后的list的元素传给x参数,然后统计x出现的次数乘上1000再
        ###减去x的ascii码,那么ascii小的字符,s.count(x)*1000-ord(x)就大,按这个表达式降序排序,
        #ss = sorted(list(set(s)), key=lambda x:s.count(x)*1000-ord(x), reverse=True)
        
    except:
        break

你可能感兴趣的:(HJ102 字符统计)