首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何统计文本中不含空格和数字的字母出现频率?

如何统计文本中不含空格和数字的字母出现频率?
EN

Stack Overflow用户
提问于 2020-03-31 09:14:12
回答 20查看 19.8K关注 0票数 5

使用字典统计输入字符串中字母的频率。只应计算字母,而不应计算空格、数字或标点符号。应将大写字母视为与小写字母相同。例如,count_letters(“这是一个句子。”)应返回{'t':2,'h':1,'i':2,'s':3,'a':1,'e':3,'n':2,'c':1}

代码语言:javascript
复制
def count_letters(text):
      result = {}
      # Go through each letter in the text
      for letter in text:
        # Check if the letter needs to be counted or not
        if letter not in result:
          result[letter.lower()] = 1
        # Add or increment the value in the dictionary
        else:
          result[letter.lower()] += 1
      return result

    print(count_letters("AaBbCc"))
    # Should be {'a': 2, 'b': 2, 'c': 2}

    print(count_letters("Math is fun! 2+2=4"))
    # Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

    print(count_letters("This is a sentence."))
    # Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}
EN

回答 20

Stack Overflow用户

回答已采纳

发布于 2020-03-31 09:20:43

这应该是可行的:

代码语言:javascript
复制
>>> from collections import Counter
>>> from string import ascii_letters
>>> def count_letters(s) :
...     filtered = [c for c in s.lower() if c in ascii_letters]
...     return Counter(filtered)
... 
>>> count_letters('Math is fun! 2+2=4')
Counter({'a': 1, 'f': 1, 'i': 1, 'h': 1, 'm': 1, 'n': 1, 's': 1, 'u': 1, 't': 1})
>>> 
票数 6
EN

Stack Overflow用户

发布于 2020-07-22 11:33:08

代码语言:javascript
复制
def count_letters(text):
  result = {}
  text = text.lower()
  # Go through each letter in the text
  for letter in text:
   
    # Check if the letter needs to be counted or not
    if letter.isalpha() :
      # Add or increment the value in the dictionary
      count = text.count(letter)
      result[letter] = count
  return result

print(count_letters("AaBbCc"))
# Should be {'a': 2, 'b': 2, 'c': 2}

print(count_letters("Math is fun! 2+2=4"))
# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}

print(count_letters("This is a sentence."))
# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}
票数 4
EN

Stack Overflow用户

发布于 2020-06-03 04:49:22

代码语言:javascript
复制
def count_letters(text):
  result = {}
  for letter in text:
    #check if it alphabet or something else
    # Check if the letter needs to be counted or not
    if letter.isalpha():
      result[letter.lower()]=result.get(letter.lower(),0)+1
    # Add or increment the value in the dictionary
  return result
代码语言:javascript
复制
print(count_letters("AaBbCc"))
# Should be {'a': 2, 'b': 2, 'c': 2}
代码语言:javascript
复制
print(count_letters("Math is fun! 2+2=4"))
# Should be {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1}
代码语言:javascript
复制
print(count_letters("This is a sentence."))
# Should be {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60941943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档