user9569944
user9569944

Reputation:

Counting only the frequency of letters in a string

I am trying to make my program count everything but numbers in a string, and store it in a dictionary.

So far I have this:

string = str(input("Enter a string: "))
stringUpper = string.upper()

dict = {}
for n in stringUpper:
    keys = dict.keys()
    if n in keys:
        dict[n] += 1
    else:
        dict[n] = 1
print(dict)

I just want the alphabetical numbers quantified, but I cannot figure out how to exclude the non-alphabetical characters.

Upvotes: 1

Views: 5997

Answers (6)

Arko Sikder
Arko Sikder

Reputation: 1

While iterating, check if the lower() and upper() is the same for a character. If they are different from each other, then it is an alphabetical letter.

if n.upper() == n.lower():
    continue

This should do it.

Upvotes: 0

MSeifert
MSeifert

Reputation: 152587

Basically there are multiple steps involved:

  • Getting rid of the chars that you don't want to count
  • Count the remaining

You have several options available to do these. I'll just present one option, but keep in mind that there might be other (and better) alternatives.

from collections import Counter

the_input = input('Enter something')
Counter(char for char in the_input.upper() if char.isalpha())

For example:

Enter something: aashkfze3f8237rhbjasdkvjuhb

Counter({'A': 3,
         'B': 2,
         'D': 1,
         'E': 1,
         'F': 2,
         'H': 3,
         'J': 2,
         'K': 2,
         'R': 1,
         'S': 2,
         'U': 1,
         'V': 1,
         'Z': 1})

So it obviously worked. Here I used collections.Counter to count and a generator expression using str.isalpha as condition to get rid of the unwanted characters.

Note that there are several bad habits in your code that will make your life more complicated than it needs to be:

  • dict = {} will shadow the built-in dict. So it's better to choose a different name.
  • string is the name of a built-in module, so here a different name might be better (but not str which is a built-in name as well).
  • stringUpper = string.upper(). In Python you generally don't use camelCase but use _ to seperate word (i.e. string_upper) but since you only use it to loop over you might as well use for n in string.upper(): directly.
  • Variable names like n aren't very helpful. Usually you can name them char or character when iterating over a string or item when iterating over a "general" iterable.

Upvotes: 2

py-D
py-D

Reputation: 669

You can check string for alphanumeric

n.isalnum()

for aphabetic:

n.isalpha()

So your code will be like:

dict = {}
for n in stringUpper:
    if n.isalpha():
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
        print(dict)
     else:
        #do something....

Upvotes: 0

Clannadqs
Clannadqs

Reputation: 307

for n in stringUpper:
    if n.isalpha()
        dict[n] += 1
    else:
        dict[n] = 1
print(dict)

Upvotes: 0

Rehan Azher
Rehan Azher

Reputation: 1340

You can use re to replace all non-alphabetical characters before doing any manipulation:

regex = re.compile('[^a-zA-Z]')
#First parameter is the replacement, second parameter is your input string
regex.sub('', stringUpper )

Upvotes: 1

czr
czr

Reputation: 658

string = str(input("Enter a string: "))

stringUpper = string.upper()

dict = {}
for n in stringUpper:
    if n not in '0123456789':
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
print(dict)

Upvotes: 0

Related Questions