赞
踩
第1关 统计字母数量
- def filename(text,n):
- with open(text,'r',encoding='utf-8') as f:
- global lst
- lst=[line.strip('\n') for line in f]
-
- def fread(long):
- string=''.join(long).lower()
- text={}
- for i in string:
- if i.isalpha():
- if i in text:
- text[i]+=1
- else:
- text[i]=1
- return text
-
- def re_sorted(dic):
- for line in 'abcdefghijklmnopqrstuvwxyz':
- counter=dic.get(line,0)
- dic[line]=counter
-
- text=dict(sorted(dic.items(),key=lambda x:(-x[1],x[0])))
- for i in text:
- print('{} 的数量是 {:>3} 个'.format(i,text[i]))
-
-
- text='step2/The Old Man and the Sea.txt'
- n=int(input())
- filename(text,n)
- dic=fread(lst[:n])
- re_sorted(dic)
</
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。