第6章 组合数据类型
1.英文字符频率统计。编写一个程序,对给定字符串出现的a-z字母频率进行分析,忽略大小写,采用降序方式输出。
- 思路:1.剔除字符串中的非字母字符,或者用一个条件语句输出字母字符。2.用字典统计各字母字符的数量。
s = input("please input a sentence:")
# To create a dictionary to collect the string and its frequency
counts = {
}
# to judge if it is a english character and record the time it presented.
if i in s:
if 97<=ord(i)<=122 or 65<=ord(i)<=90:
counts[i]=counts.get(i,0)+1
# to transfer a dictionary to a list, and to sort with its value.
items = list(counts.items())
items.sort(key=lambda x : x[1], reverse = True)
for j in