2015-03-31 39 views
0

我已經創建了一個應用程序,幫助破解學校作品的編碼單詞。我不得不使用.count()方法來計算字母列表中出現字母的次數。這是輸出:Python格式count()方法

Counter({'\n': 9, '#': 8, '&': 7, '3': 6, '*': 4, '%': 4, '1': 3, '8': 3, '0': 3, ')': 2, '+': 2, '4': 2, '7': 2, '2': 2, '-': 2, '/': 1, ',': 1, '$': 1, '6': 1, '5': 1, '!': 1, '.': 1, ':': 1, '"': 1, "'": 1, '9': 1}) 

有沒有一種方法,我可以格式化這個看起來更好,更加理解?

+0

你可以使用美麗的打印機:https://docs.python.org/2/library/pprint.html – Scironic 2015-03-31 11:08:51

+0

首先,你似乎已經使用過'collections.Counter',其次,你能解釋一下你的意思嗎?並且更容易理解*,你能舉個例子嗎? – Kasramvd 2015-03-31 11:08:57

+0

應用程序的用戶需要能夠非常理解手錶正在顯示,所以可能是這樣的: – willstaff 2015-03-31 11:10:58

回答

1
for keys, values in YourDictionaryHere.items(): 
    print keys, values 
+0

工作就像一個魅力謝謝,不知道爲什麼我沒有想到這一點。 – willstaff 2015-03-31 12:48:58

0

這是你想要的嗎?

from collections import Counter 

stuff = Counter({'\n': 9, '#': 8, '&': 7, '3': 6, '*': 4, '%': 4, '1': 3, '8': 3, '0': 3, ')': 2, '+': 2, '4': 2, '7': 2, '2': 2, '-': 2, '/': 1, ',': 1, '$': 1, '6': 1, '5': 1, '!': 1, '.': 1, ':': 1, '"': 1, "'": 1, '9': 1}) 

stuff = dict(stuff) 
line = [] 
for i,kv in enumerate(stuff.iteritems(), start=1): 
    line.append("%s : %d" % kv) 
    if i % 4 == 0: 
     print line 
     line = [] 

if line: print line 

產地:

['\n : 9', '! : 1', '# : 8', '" : 1'] 
['% : 4', '$ : 1', "' : 1", '& : 7'] 
[') : 2', '+ : 2', '* : 4', '- : 2'] 
[', : 1', '/ : 1', '. : 1', '1 : 3'] 
['0 : 3', '3 : 6', '2 : 2', '5 : 1'] 
['4 : 2', '7 : 2', '6 : 1', '9 : 1'] 
['8 : 3', ': : 1'] 

爲了獲得更多的條目,每行,改變i % 4線。