0
我有一本詞典字典,需要計算給定字符串中出現字母對的次數。我得到了字典的工作,我只是完全卡在如何使這個計數器工作...Python字典頻率
無論如何,這是我得到的。任何幫助表示讚賞
test = 'how now, brown cow, ok?'
def make_letter_pairs(text):
di = {}
total = len(text)
for i in range(len(text)-1):
ch = text[i]
ach = text[i+1]
if ch in ascii_lowercase and ach in ascii_lowercase:
if ch not in di:
row = di.setdefault(ch, {})
row.setdefault(ach, 0)
return di
make_letter_pairs(test)
難道你有 看看python中的Counter? https://docs.python.org/2/library/collections.html#collections.Counter –
我沒有。這是做到這一點的唯一方法嗎?或者可以通過添加到我的for循環? – user2951723
字母對出現多少次?該測試字符串的正確輸出是什麼? – davedwards