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.cnblogs.com/yongestcat/
欢迎转载,转载请标明出处。
如果你觉得本文还不错,对你的学习带来了些许帮助,请帮忙点击右下角的推荐
博客出处:http://www.cnblogs.com/yongestcat/
欢迎转载,转载请标明出处。
如果你觉得本文还不错,对你的学习带来了些许帮助,请帮忙点击右下角的推荐