0
点赞
收藏
分享

微信扫一扫

统计字母数量

Gascognya 2022-01-23 阅读 42
python
s = 'abcdefghijklmnopqrstuvwxyz'
n = int(input())
with open('The Old Man and the Sea.txt', 'r', encoding='utf-8') as data:
    txt = data.readlines()
if n > len(txt):
    n = len(txt)
mystr = ' '.join(txt[:n])
# print(mystr)
ls = [[x, mystr.lower().count(x)] for x in s]
ls.sort(key=lambda x: (-x[1], x[0]))
# print(ls)
for i in ls:
    print('{} 的数量是 {:>3} 个'.format(i[0], i[1]))
举报

相关推荐

0 条评论