中文字符频率统计python_python统计字符串出现最多的字母及其出现次数
統計字符串出現最多的字母及其出現次數
另外如果次數相同按字母順序排序。
方法1
可以使用自定義鍵對c.most_common()進行排序,該鍵首先考慮頻率的降序,然后考慮字母的降序(請注意lambda x: (-x[1], x[0]) ):
from collections import Counter
def ordered_letters(s, n=3):
c = Counter(s.replace(' ', ''))
top_n = sorted(c.most_common(), key=lambda x: (-x[1], x[0]))[:n]
for i, t in enumerate(top_n):
c, f = t
if i == 0: print('1st most frequent', c + '.', 'Appearances:', f)
elif i == 1: print('2nd most frequent', c + '.', 'Appearances:', f)
elif i == 2: print('3rd most frequent', c + '.', 'Appearances:', f)
else: print(str(i + 1) + 'th most frequent', c + '.', 'Appearances', f)
sent = "china construction bank"
ordered_letters(sent, 5)
# 1st most frequent n. Appearances: 4
# 2nd most frequent c. Appearances: 3
# 3rd most frequent a. Appearances: 2
# 4th most frequent i. Appearances 2
# 5th most frequent o. Appearances 2
方法2
常規方式對Counter的元組進行排序, 但第一個參數-count本身被取反。 這將產生一個反向列表,但第二個元組元素將按字母順序排序。 然后取最后n個項目。
from collections import Counter
ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(n/10%10!=1)*(n%10<4)*n%10::4])
def ordered_letters(s, n=3):
ctr = Counter(c for c in s if c.isalpha())
ctr = sorted(ctr.items(), key=lambda x: (-x[1], x[0]))[:n]
for index,value in enumerate(ctr):
print "{:s} most frequent: '{:}'. Appearances: {:}".format(ordinal(index+1),value[0],value[1])
s = "achina aconstruction banck"
ordered_letters(s, n=3)
結果:
1st most frequent: 'a'. Appearances: 4
2nd most frequent: 'c'. Appearances: 4
3rd most frequent: 'n'. Appearances: 4
要想成為python這領域的高手,必須了解和弄懂python常見問題。要我們來進行python高手修煉吧。或者你有更好的問題可以留言。
總結
以上是生活随笔為你收集整理的中文字符频率统计python_python统计字符串出现最多的字母及其出现次数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为挽回广告商,马斯克的 X 平台推出“敏
- 下一篇: SK 海力士展示全球最高层 321 层