Python3.4 分别统计字符串中26个字母的个数

题目:Define a function which, given a string argument, counts how many times each of the 26 letters occurs in the text. Lowercase and uppercase letters should be counted as the same letter! Your function should return a list of 26 numbers corresponding to the number of occurences of the 26 letters 'a' through 'z'.
function name: letter_frequency(s)

就是要定义一个 letter_frequency(s)
你输入字符串s
他返还一个列表 统计了在这个字符串中26个字母分别出现了几次 不区分大小写
求好心人帮忙 _(:зゝ∠)_
表示在下刚开始学 最好能解释一下每一步都是什么意思...嗯 以上!

import string
def letter_frequency(s):
   s = s.lower()                      #全部转小写
   l = []
   for i in string.lowercase:
      l.append(s.count(i))            #统计个数
   return l                           #返回 

   
 运行      >>> print(letter_frequency('Asdasdad'))
 结果  [3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]

追问

你是python多少啊...
我跑出来它显示
AttributeError: 'module' object has no attribute 'lowercase'

追答3.4不行吗,那你试试这个:

def letter_frequency(s):
   s = s.lower()                      #全部转小写
   l = []
   for i in [chr(x) for x in range(97,123)]:
      l.append(s.count(i))            #统计个数
   return l                           #返回

温馨提示:答案为网友推荐,仅供参考
相似回答