# #统计word中的各个字符的出现的次数, and count the number of characters used for all the first 10 names
#-*-Coding:utf-8-*-word= " Awfesdafhjkcasadckjsdackjsadvcnksausafdschfsadfdsaasdfsdacsafsdaascsaasfdasdfsdasfadfsdafsadfjtyurjryjghnkuitki "' List1 = List (word) #将字符串转化为列表while ' \ n ' in List1: #去掉列表中的 ' \ n ' character list1.remove (' \ n ') List2 = sorted (list1) # List sort List3 = List (set (List1)) #列表去重复list4 = [] #定义两个空列表list5 = []for i in list3:j = List2.count (i) #统计字符个数list4. Append ((i, J)) #将字符及个数加入新列表def recmp (x, y): #定义一个排序函数与cmp函数输出结果相反if x < y:return 1if x = = Y:return 0if x > Y:return-1list4.sort (LA MBDA x,y:recmp (x[1],y[1])) #根据第二关键词排序list5 = list4[:10] #取出前十项print list5# output result: [(' A ', '), (' s ', +), (' d ', +), (' F ', 13), (' C ', 7), (' K ', 6), (' J ', 6), (' H ', 3), (' U ', 3), (' I ', 2)]
Python experiment Two: string sorting