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}
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/yongestcat/p/13433463.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!