Complete the below function that takes a file name and a list as arguments and reads the text within the corresponding file, then creates a dictionary whose keys are the characters of the provided list, and the values are the counts of these characters within the text. If a character in the list is not included in the text, "0" should be written in the dictionary as the value of this character. The function returns the created dictionary as a result. If the file does not exist, then the function returns an empty dictionary. Here is an example case, assuming a file named "sample.txt" exists with the following content: ----- sample.txt ----- This is an example sentence. This is yet another sentence. ------------------------- >>> text2dict("sample.txt", ['a', 'b', 'c', 't']) {'a':3, 'b':0, 'c':2, 't':4} (DONT USE İNPUT .VERY İMPORTANT.)
Complete the below function that takes a file name and a list as arguments and
reads the text within the corresponding file, then creates a dictionary whose
keys are the characters of the provided list, and the values are the counts of
these characters within the text. If a character in the list is not included in
the text, "0" should be written in the dictionary as the value of this
character. The function returns the created dictionary as a result. If the file
does not exist, then the function returns an empty dictionary.
Here is an example case, assuming a file named "sample.txt" exists with the
following content:
----- sample.txt -----
This is an example sentence.
This is yet another sentence.
-------------------------
>>> text2dict("sample.txt", ['a', 'b', 'c', 't'])
{'a':3, 'b':0, 'c':2, 't':4}
(DONT USE İNPUT .VERY İMPORTANT.)
Step by step
Solved in 2 steps with 2 images