2014-04-04 36 views
0

我已經這樣做了,但這太長了,我該怎麼做一個更簡單的方法?在此先感謝你怎麼知道每個字母的頻率?

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:\n" 


"A:",letter_a,",\n" 

    "B:",letter_b,",\n" 

    "C:",letter_c,",\n" 

    "D:",letter_d,",\n" 

    "E:",letter_e,",\n" 

    "F:",letter_f,",\n" 

    "G:",letter_g,",\n" 

    "H:",letter_h,",\n" 

    "I:",letter_i,",\n" 

    "J:",letter_j,",\n" 

    "K:",letter_k,",\n" 


    "L:",letter_l,",\n" 

    "M:",letter_m,",\n" 

    "N:",letter_n,",\n" 

    "O:",letter_o,",\n" 

    "P:",letter_p,",\n" 

    "Q:",letter_q,",\n" 

    "R:",letter_r,",\n" 

    "S:",letter_s,",\n" 

    "T:",letter_t,",\n" 

    "U:",letter_u,",\n" 


    "V:",letter_v,",\n" 

    "W:",letter_w,",\n" 

    "X:",letter_x,",\n" 

    "Y:",letter_y,",\n" 

    "Z:",letter_z, 

    "\n") 
+0

你不應該至少把你正在使用的語言? – aleation

+0

它看起來像Python,但OP應該確認。 – elbear

+0

也http://stackoverflow.com/questions/10806866/counting-each-letters-frequency-in-a-string – devnull

回答

0

當然,您可以將代碼減少到幾乎兩行。但是可讀性可能是,如果你不知道Python語法

import string 
all_words = 'this is me' 
print("there is:\n {0}".format('\n'.join([letter+':'+str(all_words.count(letter) + all_words.count(letter.lower())) for letter in string.uppercase]))) 
7

有各種各樣的答案 - 當然,因爲你對你應該一直想着第十一次寫了letter_X = all_words.count('X')「也許for循環會救我脫離這?」並會:

import string 

for character in string.ascii_lowercase: 
    ... 

同理:

  • 「不是很多獨立的變量,我可以用一個dict以字母爲關鍵和計數的價值?」
  • 「我需要所有這些,然後print他們,或者我能不能print他們馬上?」

然而,在這裏做最簡單的事情是用collections.Counter,例如:

>>> 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 

這樣,你只處理字符串一次,而不是count荷蘭國際集團的每一個字母。 Counter基本上是一個具有一些額外的有用功能的字典。

另外,您需要考慮的情況 - "A"應該計爲"a",反之亦然,還是它們是分開的?

0

用於循環。例如for循環可以計算字母'a'和'b':

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

如果您不希望用戶collection.Counter和其他庫(如string.ascii_uppercase),並建立自己的算法的問題,你可以試試這個:

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)) 
相關問題