Counting the number of letter characters in a string - python

I need to have a user enter a string in which any character is allowed. Once entered I need to count each letter character occurrence in the string. So far I have:
s = input("Enter a string: ")
s = s.upper()
all_freq = {}
for i in s:
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
print(all_freq)
This is wrong because it includes numbers, spaces and special characters in the count.

Using a list comprehension to filter and Counter (from collections) to count would make it more compact:
from collections import Counter
s = "Hello World!"
result = Counter(c for c in s.upper() if c.isalpha())
print(result)
# Counter({'L': 3, 'O': 2, 'H': 1, 'E': 1, 'W': 1, 'R': 1, 'D': 1})

If you only want to count characters, you can use the isalpha function to check if a character is alphabetical.
s = input("Enter a string: ")
s = s.upper()
all_freq = {}
for i in s:
if i.isalpha():
if i in all_freq:
all_freq[i] += 1
else:
all_freq[i] = 1
print(all_freq)
Enter a string: Hello World!
{'H': 1, 'E': 1, 'L': 3, 'O': 2, 'W': 1, 'R': 1, 'D': 1}
Hope this helps!

You can also use regex expression to check for characters
import re
s = input("Enter a string: ")
s = s.upper()
all_freq = {}
for i in s:
if bool(re.match('[A-Z]',i)):
if i in all_freq :
all_freq[i] += 1
else:
all_freq[i] = 1
print(all_freq)
Here's the output:
Enter a string: hello hello 1 $
{'H': 2, 'E': 2, 'L': 4, 'O': 2}

Maybe one simple solution is to use string module like bellow:
import string
s = input("Enter a string: ")
ignore = string.whitespace + string.digits + string.punctuation
s = s.upper()
all_freq = {}
for c in s:
if c not in ignore:
all_freq[c] = all_freq.get(c, 0) + 1
print(all_freq)

Related

Ignore Whitespace while counting number of characters in a String

I am trying to write a function which will count the number of characters present in an input string and store as key-value in a dictionary.The code is partially working i.e it is also counting the whitespaces present in between 2 words.How do I avoid counting the whitespaces?
#Store Characters of a string in a Dictionary
def char_dict(string):
char_dic = {}
for i in string:
if i in char_dic:
char_dic[i]+= 1
else:
char_dic[i]= 1
return char_dic
print(char_dict('My name is Rajib'))
You could just continue if the character is a white space:
def char_dict(string):
char_dic = {}
for i in string:
if ' ' == i:
continue
if i in char_dic:
char_dic[i] += 1
else:
char_dic[i]= 1
return char_dic
print(char_dict('My name is Rajib')) # {'j': 1, 'm': 1, 'M': 1, 'i': 2, 'b': 1, 'e': 1, 'a': 2, 'y': 1, 'R': 1, 'n': 1, 's': 1}
A cleaner solution would be:
from collections import defaultdict
def countNonSpaceChars(string):
charDic = defaultdict(lambda: 0)
for char in string:
if char.isspace():
continue
charDic[char] += 1
return dict(charDic)
print(countNonSpaceChars('My name is Rajib')) # {'i': 2, 'a': 2, 'R': 1, 'y': 1, 'M': 1, 'm': 1, 'e': 1, 'n': 1, 'j': 1, 's': 1, 'b': 1}
You can delete space -> string = string.replace (" ","")
def char_dict(string):
char_dic = {}
string=string.replace(" ","")
for i in string:
if i in char_dic:
char_dic[i]+= 1
else:
char_dic[i]= 1
return char_dic
print(char_dict('My name is Rajib'))
To simplify things for you, there's a library called collections that has a Counter function that will produce a dictionary of values and their occurrences in a string. Then, I would simply remove the whitespace key from the dictionary if it is present using the del keyword.
from collections import Counter
def char_dict(string):
text = 'My name is Rajib'
c = Counter(text)
if ' ' in c: del c[' ']
print(char_dict('My name is Rajib'))
This method is very readable and doesn't require too much reinventing.

Counts the frequency of each character using the dictionary python 3.8 [duplicate]

This question already has answers here:
Counting letters with python
(3 answers)
Closed 2 years ago.
I have to write a Python program that takes a string as an input from the user and
I have this string:
Python programming is fun
I want to count the frequency of each character using the dictionary which should be:
{'p': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 3, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 2, 's': 1, 'f': 1, 'u': 1}
I tried this:
string = input("Enter a string: ")
new_dict = {} #new_dict for frequency
lower_case = string.lower()
for key in lower_case:
if key in new_dict.keys():
new_dict[key] = new_dict[key] + 1
else:
new_dict[key] = 1
print(new_dict)
and the output came:
{'p': 2, 'y': 1, 't': 1, 'h': 1, 'o': 2, 'n': 3, ' ': 3, 'r': 2, 'g': 2, 'a': 1, 'm': 2, 'i': 2, 's': 1, 'f': 1, 'u': 1}
Here output is counting the spaces in the line. I want to remove the space from the output. What should I apply?
you can check if the character is a space, and if it is return to top of the loop
string = input("Enter a string: ")
new_dict = {} #new_dict for frequency
lower_case = string.lower()
for key in lower_case:
if key == " ":
continue # return to top of loop
if key in new_dict.keys():
new_dict[key] = new_dict[key] + 1
else:
new_dict[key] = 1
print(new_dict)
Or just remove the key after counter all characters (including spaces) by doing new_dict.pop(" ") after the loop.
You should remove the spaces from input_string, like
string = #input("Enter a string: ")
new_dict = {} #new_dict for frequency
lower_case = string.lower().replace(' ', '')
for key in lower_case:
if key in new_dict.keys():
new_dict[key] = new_dict[key] + 1
else:
new_dict[key] = 1
print(new_dict)
You can do string.replace(' ', '') and delete the spaces from the string.
you can replace the white-space at the very beginning:
string = input("Enter a string: ").replace(' ', '')
new_dict = {} # new_dict for frequency
lower_case = string.lower()
for key in lower_case:
if key in new_dict.keys():
new_dict[key] = new_dict[key] + 1
else:
new_dict[key] = 1
print(new_dict)
Use .replace() to remove the spaces. You can use defaultdict to make the code more concise:
from collections import defaultdict
string = input("Enter a string: ").lower().replace(" ", "")
new_dict = defaultdict(lambda: 0)
for key in string:
new_dict[key] += 1

Python: regex condition to find lower case/digit before capital letter

I would like to split a string in python and make it into a dictionary such that a key is any chunk of characters between two capital letters and the value should be the number of occurrences of these chunk in the string.
As an example: string = 'ABbACc1Dd2E' should return this: {'A': 2, 'Bb': 1, 'Cc1': 1, 'Dd2': 1, 'E': 1}
I have found two working solution so far (see below), but I am looking for a more general/elegant solution to this, possibly a one-line regex condition.
Thank you
Solution 1
string = 'ABbACc1Dd2E'
string = ' '.join(string)
for ii in re.findall("([A-Z] [a-z])",string) + \
re.findall("([A-Z] [0-9])",string) + \
re.findall("([a-x] [0-9])",string):
new_ii = ii.replace(' ','')
string = string.replace(ii, new_ii)
string = string.split()
all_dict = {}
for elem in string:
all_dict[elem] = all_dict[elem] + 1 if elem in all_dict.keys() else 1
print(all_dict)
{'A': 2, 'Bb': 1, 'Cc1': 1, 'Dd2': 1, 'E': 1}
Solution 2
string = 'ABbACc1Dd2E'
all_upper = [ (pos,char) for (pos,char) in enumerate(string) if char.isupper() ]
all_dict = {}
for (pos,char) in enumerate(string):
if (pos,char) in all_upper:
new_elem = char
else:
new_elem += char
if pos < len(string) -1 :
if string[pos+1].isupper():
all_dict[new_elem] = all_dict[new_elem] + 1 if new_elem in all_dict.keys() else 1
else:
pass
else:
all_dict[new_elem] = all_dict[new_elem] + 1 if new_elem in all_dict.keys() else 1
print(all_dict)
{'A': 2, 'Bb': 1, 'Cc1': 1, 'Dd2': 1, 'E': 1}
Thanks to usr2564301 for this suggestion:
The right regex is '[A-Z][a-z]*\d*'
import re
string = 'ABbACc1Dd2E'
print(re.findall(r'[A-Z][a-z]*\d*', string))
['A', 'Bb', 'A', 'Cc1', 'Dd2', 'E']
One can then use itertools.groupby to make an iterator that returns consecutive keys and groups from the iterable.
from itertools import groupby
all_dict = {}
for i,j in groupby(re.findall(r'[A-Z][a-z]*\d*', string)):
all_dict[i] = all_dict[i] + 1 if i in all_dict.keys() else 1
print(all_dict)
{'A': 2, 'Bb': 1, 'Cc1': 1, 'Dd2': 1, 'E': 1}
Ultimately, one could use sorted() to get this in one line with the correct counting:
print({i:len(list(j)) for i,j in groupby(sorted(re.findall(r'[A-Z][a-z]*\d*', string))) })
{'A': 2, 'Bb': 1, 'Cc1': 1, 'Dd2': 1, 'E': 1}

How to form a dictionary from a string?

word = 'stacks'
word_dict = {} # to form new dictionary formed from
for letter in word:
word_dict[letter] += 1
print word_dict
I want to create a new dictionary from a string, tracking the count of the letters from word. So what I'm trying to get is:
> word_dict = {'s':2, 't':1, 'a':1, 'c':1, 'k':1}
But I can't figure out how to do this. I get KeyError with my current code
Use the collections.Counter() class instead:
from collections import Counter
word_dict = Counter(word)
The Counter does the exact same thing; count occurrences of each letter in word.
In your specific case you didn't first check if the key already exists or provide a default if it doesn't. You could use dict.get() to do that:
word = 'stacks'
word_dict = {} # to form new dictionary formed from
for letter in word:
word_dict[letter] = word_dict.get(letter, 0) + 1
print word_dict
or use dict.setdefault() separately to explicitly set a default before incrementing:
word = 'stacks'
word_dict = {} # to form new dictionary formed from
for letter in word:
word_dict.setdefault(letter, 0)
word_dict[letter] += 1
print word_dict
or test for the key yourself:
word = 'stacks'
word_dict = {} # to form new dictionary formed from
for letter in word:
if letter not in word_dict:
word_dict[letter] = 0
word_dict[letter] += 1
print word_dict
in decreasing order of efficiency.
Or you could use a collections.defaultdict() object to automatically insert a 0 if there the key doesn't yet exist:
from collections import defaultdict
word_dict = defaultdict(int)
for letter in word:
word_dict[letter] += 1
print word_dict
This is essentially what the Counter class does, but the type adds some other niceties such as listing the most common keys or combining counters.
Demo:
>>> from collections import defaultdict, Counter
>>> word = 'stacks'
>>> word_dict = {} # to form new dictionary formed from
>>> for letter in word:
... word_dict[letter] = word_dict.get(letter, 0) + 1
...
>>> word_dict
{'a': 1, 'c': 1, 's': 2, 't': 1, 'k': 1}
>>> word_dict = defaultdict(int)
>>> for letter in word:
... word_dict[letter] += 1
...
>>> word_dict
defaultdict(<type 'int'>, {'a': 1, 'c': 1, 's': 2, 't': 1, 'k': 1})
>>> Counter(word)
Counter({'s': 2, 'a': 1, 'c': 1, 't': 1, 'k': 1})
Try this
from collections import Counter
>>>Counter(word)
Counter({'s': 2, 'a': 1, 'c': 1, 't': 1, 'k': 1})

Python Counting Vowels

I have started on a program to count vowels and have seemed to be getting nowhere. I need to count vowels from a string and then display the vowels. I need to do this by storing the number of occurrences in variables. Like this :
a = 0
b = 0
....
then print the lowest.
Current code (its not that much ):
string = str(input("please input a string: "))
edit= ''.join(string)
print(edit)
I have tried a number of methods just by my self and don't seem to get anywhere.
You could use a dictionary comprehension:
>>> example = 'this is an example string'
>>> vowel_counts = {c: example.count(c) for c in 'aeoiu'}
>>> vowel_counts
{'i': 2, 'o': 0, 'e': 5, 'u': 0, 'a': 2}
Then finding the minimum, maximum etc. is trivial.
>>> a="hello how are you"
>>> vowel_count = dict.fromkeys('aeiou',0)
>>> vowel_count
{'a': 0, 'i': 0, 'e': 0, 'u': 0, 'o': 0}
>>> for x in 'aeiou':
... vowel_count[x]=a.count(x)
...
>>> vowel_count
{'a': 1, 'i': 0, 'e': 2, 'u': 1, 'o': 3}
now from here you can print low nd max
You can use dictionary for this problem. Iterate over each character and if the character is a vowel, put it in dictionary with count 0 and increment its count by 1, and for every next occurrence keep incrementing the count.
>>> string = str(input("please input a string: "))
please input a string: 'Hello how are you'
>>> dt={} # initialize dictionary
>>> for i in string: # iterate over each character
... if i in ['a','e','i','o','u']: # if vowel
... dt.setdefault(i,0) # at first occurrence set count to 0
... dt[i]+=1 # increment count by 1
...
>>> dt
{'a': 1, 'u': 1, 'e': 2, 'o': 3}
word = input('Enter Your word : ')
vowel = 'aeiou'
vowel_counter = {}
for char in word:
if char in vowel:
vowel_counter[char] = vowel_counter.setdefault(char,0)+1
sorted_result = sorted(vowel_counter.items(), reverse=True,key=lambda x : x[1])
for key,val in sorted_result:
print(key,val)

Categories