Python experiment Two: string sorting

Source: Internet
Author: User

# #统计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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.