2017-04-18 105 views
-1

我需要將字體內的26個字母中的每個字母的計數累加到字典中。當用戶輸入一個字母時,我需要在文本中顯示該字母的頻率。我該怎麼做呢?如何從字典中獲取密鑰python

這是我到目前爲止的代碼:

import urllib2 
import numpy as py 
import matplotlib 

response = urllib2.urlopen('http://students.healthinformaticshub.ca/jane-austen-sense-n-sensibility.txt') 

alphabet = 'abcdefghijklmnopqrstuvwxyz' 

# initialize the dict we will use to store our 
# counts for the individual vowels: 
alphabet_counts = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0,\ 
'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0,\ 
't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0} 

total_letter_count = 0 

# loop thru line by line: 
for line in response: 
    line = line.lower() 

    for ch in line: 
     if ch in alphabet: 
      alphabet_counts[ch] += 1 
      total_letter_count += 1 


print('# of a\'s: ' + str(alphabet_counts['a'])) 
print('# of b\'s: ' + str(alphabet_counts['b'])) 
print('# of c\'s: ' + str(alphabet_counts['c'])) 
print('# of d\'s: ' + str(alphabet_counts['d'])) 
print('# of e\'s: ' + str(alphabet_counts['e'])) 
print('# of f\'s: ' + str(alphabet_counts['f'])) 
print('# of g\'s: ' + str(alphabet_counts['g'])) 
print('# of h\'s: ' + str(alphabet_counts['h'])) 
print('# of i\'s: ' + str(alphabet_counts['i'])) 
print('# of j\'s: ' + str(alphabet_counts['j'])) 
print('# of k\'s: ' + str(alphabet_counts['k'])) 
print('# of l\'s: ' + str(alphabet_counts['l'])) 
print('# of m\'s: ' + str(alphabet_counts['m'])) 
print('# of n\'s: ' + str(alphabet_counts['n'])) 
print('# of o\'s: ' + str(alphabet_counts['o'])) 
print('# of p\'s: ' + str(alphabet_counts['p'])) 
print('# of q\'s: ' + str(alphabet_counts['q'])) 
print('# of r\'s: ' + str(alphabet_counts['r'])) 
print('# of s\'s: ' + str(alphabet_counts['s'])) 
print('# of t\'s: ' + str(alphabet_counts['t'])) 
print('# of u\'s: ' + str(alphabet_counts['u'])) 
print('# of v\'s: ' + str(alphabet_counts['v'])) 
print('# of w\'s: ' + str(alphabet_counts['w'])) 
print('# of x\'s: ' + str(alphabet_counts['x'])) 
print('# of y\'s: ' + str(alphabet_counts['y'])) 
print('# of z\'s: ' + str(alphabet_counts['z'])) 


resp = ''' 
1.) Find probability of a particular letter of the alphabet 
2.) Show the barplot representing these probabilities for the entire alphabet 
3.) Save that barplot as a png file 
4.) Quit''' 
+0

看一看類['collections.Counter'](https://docs.python.org/ 2 /庫/ collections.html#collections.Counter)。您應該可以使用它來替換整個內部循環,然後丟棄字典中不包含的所有鍵。或者,只需將行傳遞到計數器時從行中過濾出非字母字符即可。 –

回答

0

我不知道你真正想要的,因爲你只是學習。我會給你一個提示而不是答案。 A dict對象有一個稱爲itemsiteritems的方法。獲取鍵和值。爲了計算得到一個給定的字符的概率可以用iteritems:

char_probabilities = dict() 
for character, count in alphabet_counts.iteritems(): 
    # compute probability given the 
    # frequency of character her you can use the 
    # sum builtin and values method on the dict 
    char_probabilities[character] = [YOU DO SOME WORK] 
0

我清理了一些代碼的,我還增加了如何使用raw_input(因爲你是根據使用Python 2.7的例子你使用的urllib2的):

import urllib2 
import numpy as py 
import matplotlib 

response = urllib2.urlopen('http://students.healthinformaticshub.ca/jane-austen-sense-n-sensibility.txt') 

alphabet = 'abcdefghijklmnopqrstuvwxyz' 

# initialize the dict we will use to store our 
# counts for the individual vowels: 
alphabet_counts = {letter: 0 for letter in alphabet} 

total_letter_count = 0 

# loop thru line by line: 
for line in response: 
    line = line.lower() 

    for ch in line: 
     if ch in alphabet: 
      alphabet_counts[ch] += 1 
      total_letter_count += 1 

for letter in alphabet_counts: 
    print('# of ' + letter + '\'s: ' + str(alphabet_counts[letter])) 

letter = raw_input("Enter a character: ") 
print('# of ' + letter + '\'s: ' + str(alphabet_counts[letter])) 

resp = ''' 
1.) Find probability of a particular letter of the alphabet 
2.) Show the barplot representing these probabilities for the entire alphabet 
3.) Save that barplot as a png file 
4.) Quit''' 
0

這似乎是一個教科書使用Counter類:

import collections, urllib2, contextlib 

url = 'http://students.healthinformaticshub.ca/jane-austen-sense-n-sensibility.txt' 
alphabet_counts = collections.Counter() 
with contextlib.closing(urllib2.urlopen(url)) as response: 
    for line in response: 
     alphabet_counts.update(x for x in line.lower() if x.isalpha()) 

該計數器爲dict子類將表現得與您原來的alphabet_counts一樣,但您的努力要少得多。請記住,您可能想要關閉輸入流,這就是我使用with塊的原因。

要獲得頻率,你需要知道計數器值的總和:

total_letters = sum(alphabet_counts.values()) 
frequencies = {letter: float(count)/total_letters for count, letter in alphabet_counts.iteritems()}