统计字母数量

Gascognya

关注

阅读 43

2022-01-23

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)

0 0 举报