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.
Related
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
I was wondering if anyone could help me out.
How do I get this code to record ONLY the frequency of letters in a text file into a dictionary (does NOT count spaces, line, numbers, etc)?
Additionally how do I divide each letter by the total letters to report the percent frequency of each letter in the file?
This is what I have currently:
def linguisticCalc():
"""
Asks user to input a VALID filename. File must be a text file. IF valid, returns the frequency of ONLY letters in file.
"""
filename = input("Please type your VALID filename")
if os.path.exists(filename) == True:
with open(filename, 'r') as f:
f_content = f.read()
freq = {}
for i in f_content:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
print(str(freq))
else:
print("This filename is NOT valid. Use the getValidFilename function to test inputs.")
Something that might help you determine whether the character in question is a letter, is this:
import string
# code here
if character in string.ascii_letters:
# code here
Check out collections.Counter()
You can use it to Count every letter in a string:
Counter('Articles containing potentially dated statements from 2011')
It gives this output, which is useful for counting characters in a string:
Counter({'A': 1,
'r': 2,
't': 8,
'i': 4,
'c': 2,
'l': 3,
'e': 5,
's': 3,
' ': 6,
'o': 3,
'n': 5,
'a': 4,
'g': 1,
'p': 1,
'y': 1,
'd': 2,
'm': 2,
'f': 1,
'2': 1,
'0': 1,
'1': 2})
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)
This question already has answers here:
Counting each letter's frequency in a string
(2 answers)
Closed 4 years ago.
How do I create a function that will let me input a word, and it will execute to create a dictionary that counts individual letters in the code. I would want it to display as a dictionary, for example, by inputting 'hello' it will display {'e': 1, 'h': 1, 'l': 2, 'o': 1}
I AM ALSO required to have 2 arguments in the function, one for the string and one for the dictionary. THIS IS DIFFERENT to the "Counting each letter's frequency in a string" question.
For example, I think I would have to start as,
d = {}
def count(text, d ={}):
count = 0
for l in text:
if l in d:
count +=1
else:
d.append(l)
return count
But this is incorrect? Also Would i need to set a default value to text, by writing text ="" in case the user does not actually enter any word?
Furthermore, if there were existing values already in the dictionary, I want it to add to that existing list. How would this be achieved?
Also if there were already existing words in the dictionary, then how would you add onto that list, e.g. dct = {'e': 1, 'h': 1, 'l': 2, 'o': 1} and now i run in terminal >>> count_letters('hello', dct) the result would be {'e': 2, 'h': 2, 'l': 4, 'o': 2}
If you can use Pandas, you can use value_counts():
import pandas as pd
word = "hello"
letters = [letter for letter in word]
pd.Series(letters).value_counts().to_dict()
Output:
{'e': 1, 'h': 1, 'l': 2, 'o': 1}
Otherwise, use dict and list comprehensions:
letter_ct = {letter:0 for letter in word}
for letter in word:
letter_ct[letter] += 1
letter_ct
You can use pythons defaultdict
from collections import defaultdict
def word_counter(word):
word_dict = defaultdict(int)
for letter in word:
word_dict[letter] += 1
return(word_dict)
print(word_counter('hello'))
Output:
defaultdict(<class 'int'>, {'h': 1, 'e': 1, 'l': 2, 'o': 1})
def count_freqs(string, dictionary={}):
for letter in string:
if letter not in dictionary:
dictionary[letter] = 1
else:
dictionary[letter] += 1
return dictionary
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)