Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python codes for counting the occurrence of a letters in dictionary excluding digits

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}
Comment

PREVIOUS NEXT
Code Example
Python :: python dictionary add item 
Python :: python dynamic variable name 
Python :: pyqt math 
Python :: dictionary.com 
Python :: gcd function in python 
Python :: dm command in discord.py 
Python :: expand pandas dataframe into separate rows 
Python :: one-hot encode categorical variables standardize numerical variables 
Python :: django migrations 
Python :: class __call__ method python 
Python :: find index of value in list python 
Python :: create a dictionary from index and column pandas 
Python :: How to add all the numbers of a list using python? 
Python :: unicodedata no accent 
Python :: Python NumPy tile Function Example 
Python :: python empty list 
Python :: check if text is python 
Python :: how to len in the pythin 
Python :: python check empty string 
Python :: picture plot 
Python :: Python NumPy expand_dims Function Example 
Python :: django create multiple objects 
Python :: how to implement dfa in python 
Python :: how to store the variable in dictionary 
Python :: tensorflow data augmentation 
Python :: how to import somthing from another directory in pyhon 
Python :: object oriented programming python 
Python :: Remove an element from a Python list Using remove() method 
Python :: __dict__ 
Python :: length of list in python 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =