A function called freq dictionary with a single parameter infile, the name of th
ID: 3562704 • Letter: A
Question
A function called freq dictionary with a single parameter infile, the name of the text file for which we want to compute the frequency distribution. The function should first open infile for reading. It should then create a dictionary with key:value pairs, where the key is a character occurring in infile and its associated value is the frequency of that character in the file (i.e., the number of times it occurs in the file). Note that every non-whitespace character (characters other than newline, space, and tab) occurring in the file should be included in the dictionary. Upper and lower case letters should be kept distinct. Characters that do not occur in the file should not be included in the dictionary. The function must return the dictionary. Remember to close the file prior to the return statement.
Explanation / Answer
def freq_dictionary(filename):
file = open(filename)
data = file.read()
dict = {}
for i in file:
if i != '':
dict[i] = dict.get(i,0)+1
file.close()
return dict
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.