python計算字母出現次數
pyschools Topic 8: Question 7的題目:給定一個單詞,輸出字母及字母出現次數
Write a function countLetters(word) that takes in a word as argument and returns a dictionary that counts the number of times each letter appears.
python真是方便,可以一行代碼搞定
def countLetters(word):
return dict(sorted([(l,word.count(l)) for l in set(word)]))
print(countLetters('google'))
{'e': 1, 'g': 2, 'l': 1, 'o': 2}
作者:九命貓幺
博客出處:http://www.rzrgm.cn/yongestcat/
歡迎轉載,轉載請標明出處。
如果你覺得本文還不錯,對你的學習帶來了些許幫助,請幫忙點擊右下角的推薦
博客出處:http://www.rzrgm.cn/yongestcat/
歡迎轉載,轉載請標明出處。
如果你覺得本文還不錯,對你的學習帶來了些許幫助,請幫忙點擊右下角的推薦

浙公網安備 33010602011771號