关于python:你怎么知道每个字母的频率?

How do you know the frequency of each letters?

本问题已经有最佳答案,请猛点这里访问。

我已经做了,但是时间太长了,我怎么能做一个更简单的方法呢?提前谢谢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
letter_a = all_words.count('a')

letter_b = all_words.count('b')

letter_c = all_words.count('c')

letter_d = all_words.count('d')

letter_e = all_words.count('e')

letter_f = all_words.count('f')

letter_g = all_words.count('g')

letter_h = all_words.count('h')

letter_i = all_words.count('i')

letter_j = all_words.count('j')

letter_k = all_words.count('k')

letter_l = all_words.count('l')

letter_m = all_words.count('m')

letter_n = all_words.count('n')

letter_o = all_words.count('o')

letter_p = all_words.count('p')

letter_q = all_words.count('q')

letter_r = all_words.count('r')

letter_s = all_words.count('s')

letter_t = all_words.count('t')

letter_u = all_words.count('u')

letter_v = all_words.count('v')

letter_w = all_words.count('w')

letter_x = all_words.count('x')

letter_y = all_words.count('y')

letter_z = all_words.count('z')



print("There is:
"



"A:",letter_a,",
"


 "B:",letter_b,",
"


 "C:",letter_c,",
"


 "D:",letter_d,",
"


 "E:",letter_e,",
"


 "F:",letter_f,",
"


 "G:",letter_g,",
"


 "H:",letter_h,",
"


 "I:",letter_i,",
"


 "J:",letter_j,",
"


 "K:",letter_k,",
"



 "L:",letter_l,",
"


 "M:",letter_m,",
"


 "N:",letter_n,",
"


 "O:",letter_o,",
"


 "P:",letter_p,",
"


 "Q:",letter_q,",
"


 "R:",letter_r,",
"


 "S:",letter_s,",
"


 "T:",letter_t,",
"


 "U:",letter_u,",
"



 "V:",letter_v,",
"


 "W:",letter_w,",
"


 "X:",letter_x,",
"


 "Y:",letter_y,",
"


 "Z:",letter_z,

 "
"
)


有各种各样的答案——当然,正如你第十次写的那样,你应该一直在想"也许一个for循环可以救我脱离这个困境?"它会:

1
2
3
4
import string  

for character in string.ascii_lowercase:
    ...

类似地:

  • "我可以用一个以字母为键,以计数为值的dict来代替许多单独的变量吗?"
  • "我需要把这些东西都存起来吗,那么我可以直接把它们存起来吗?"

但是,这里要做的最简单的事情是使用collections.Counter,例如:

1
2
3
4
5
6
7
8
>>> from collections import Counter
>>> counter = Counter("foo bar baz")
>>> counter
Counter({'a': 2, ' ': 2, 'b': 2, 'o': 2, 'f': 1, 'r': 1, 'z': 1})
>>> counter['a']
2
>>> counter['c']
0

这样,您只需处理字符串一次,而不是对每个字母使用countCounter基本上是一本具有一些额外有用功能的字典。

此外,您还需要考虑这种情况——应将"A"算作"A",反之亦然,还是它们是分开的?


如果不想使用collection.Counter和其他库(如string.ascii_uppercase)并构建自己的算法,可以尝试以下方法:

1
2
3
4
5
6
7
8
all_words = 'asasasaassasaasasasasassa'
upper_words = all_words.upper()
letter_freq = {}
for letter in set(upper_words):
    l etter_freq[letter] = upper_words.count(letter)

for letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
    print '%s: %d'%(letter, letter_freq.get(letter, 0))


当然,您可以将代码减少到几乎两行。但是,如果您不知道Python语法,可读性可能是一个问题。

1
2
3
4
5
import string
all_words = 'this is me'
print("there is:
 {0}"
.format('
'
.join([letter+':'+str(all_words.count(letter) + all_words.count(letter.lower())) for letter in string.uppercase])))

循环使用。for循环的一个示例,它将计算字母"a"和"b":

1
2
for character in"ab":
    print(character +" has" + str(all_words.count(character)) +" occurences.")