Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to count the frequency of letters in text excluding whitespace and numbers?

Tags:

python

Use a dictionary to count the frequency of letters in the input string. Only letters should be counted, not blank spaces, numbers, or punctuation. Upper case should be considered the same as lower case. For example, count_letters("This is a sentence.") should return {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1}

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}
like image 835
Yahia Naeem Avatar asked Mar 31 '20 01:03

Yahia Naeem


People also ask

How do you count the frequency of characters in a string in Java?

Freq will be used to maintain the count of each character present in the string. Now, iterate through the string to compare each character with rest of the string. Increment the count of corresponding element in freq. Finally, iterate through freq to display the frequencies of characters.

How do you count the number of letter spaces for a string in Python?

First we find all the digits in string with the help of re. findall() which give list of matched pattern with the help of len we calculate the length of list and similarly we find the total letters in string with the help of re. findall() method and calculate the length of list using len.

How do you count the number of characters in a string except spaces in Python?

Use str. count() to count the number of characters in a string except spaces. Use len(object) to get the length of a string object .


3 Answers

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}
like image 77
AKinsoji Hammed Adisa Avatar answered Oct 22 '22 10:10

AKinsoji Hammed Adisa


This should work:

>>> 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})
>>> 
like image 44
lenik Avatar answered Oct 22 '22 11:10

lenik


So I got your question,

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 in "abcdefghijklmnopqrstuvwxyz":
      #
      if letter not in result:
        result[letter] = 1
      # Add or increment the value in the dictionary
      else:
        result[letter] += 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}
like image 22
Shobhit Umrao Avatar answered Oct 22 '22 10:10

Shobhit Umrao