python 查询一段字符中所有字母出现的次数,并倒序列出

 

 1 # coding:utf-8
 2 
 3 s = """
 4 Have you thought about what you want people to say about you after you’re gone? Can you hear the voice saying, “He was a great man.” Or “She really will be missed.” What else do they say?
 5 One of the strangest phenomena of life is to engage in a work that will last long after death. Isn’t that a lot like investing all your money so that future generations can bare interest on it? Perhaps, yet if you look deep in your own heart, you’ll find something drives you to make this kind of contribution---something drives every human being to find a purpose that lives on after death.
 6 """
 7 
 8 dit1 = {}
 9 for i in s:
10     dit1[i] = s.count(i)
11 #print dit1
12 
13 c1 = 0
14 dit2 = {}
15 for ii in dit1.keys():
16     if ii.isalpha() is True:
17         c1 = c1 +1
18         dit2[ii] = dit1[ii]
19 
20 print "all these have %d letter" % c1
21 print sorted(dit2.items(),key=lambda d:d[1],reverse=True)

 最后一句话是经常用于对dict类型进行排序的方法,其中reverse=True是倒序排列

转载于:https://www.cnblogs.com/ZHshuang463508120/p/4701637.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值