python-统计文件中字母出现次数

 

# 菜鸟出没
# 非战斗人员请撤离
# 时间:2022/3/16 17:47
# 统计大写字母出现的次数,并按照字母出现次数降序排序输出


def countchar(file):
    fp = open(file, mode='r', encoding='utf-8')
    ch = fp.read()
    l = {}
    for i in ch:
        if i.isupper():
            if i in l:
                l[i] += 1
            else:
                l[i] = 1
    list1 = sorted(l.items(), key=lambda e: e[1], reverse=True)
    for i,j in list1:
        print((i,j))
        
        
file = input()
countchar(file)

你可能感兴趣的:(python)