![taylorrcho977](/avatars/40238.jpg)
taylorrcho977
21.03.2020 •
Computers and Technology
The following function open_file() opens a file called 'example.txt' and returns the file pointer. This function is called within main and the file pointer is used to read lines in the file. Create a dictionary called 'dict_of_words' that have words as 'keys' and (integer) counters as values. The counter values keep a count of the number of times a word has appeared in the text file. In the end, store the dictionary key, values in a list, sort and print the list on the screen.
Note that the counts are not case-sensitive, that is, 'Word' is the same as 'word' or 'wORd'.
Also, note that your program should account for if a ',' (comma) separates two words, e.g. 'food, water, electricity'
Example:
Contents of input text file:
I do not think there is any thrill that can go through the human heart like that felt by the inventor as he sees some creation of the brain unfolding to success such emotions make a man forget food sleep friends love everything.
Nikola Tesla
Output:
[('a', 1), ('any', 1), ('as', 1), ('brain', 1), ('by', 1), ('can', 1), ('creation', 1), ('do', 1), ('emotions', 1), ('everything', 1), ('felt', 1), ('food', 1), ('forget', 1), ('friends', 1), ('go', 1), ('he', 1), ('heart', 1), ('human', 1), ('i', 1), ('inventor', 1), ('is', 1), ('like', 1), ('love', 1), ('make', 1), ('man', 1), ('nikola', 1), ('not', 1), ('of', 1), ('sees', 1), ('sleep', 1), ('some', 1), ('success', 1), ('such', 1), ('tesla', 1), ('that', 2), ('the', 3), ('there', 1), ('think', 1), ('thrill', 1), ('through', 1), ('to', 1), ('unfolding', 1)]
Use the code below:
def open_file():
fpointer = open('example.txt')
return fpointer
def main():
dictlist = []
fp = open_file()
#loop to iterate over lines in file
for key, value in dict_of_words.items():
temp = (key,value)
dictlist.append(temp)
print(sorted(dictlist))
main()
Solved
Show answers
More tips
- S Sport When and Where Will the 2014 World Cup be Held?...
- C Computers and Internet How to Choose a Monitor?...
- S Style and Beauty How to Get Rid of Peeling Nails: Natural Remedies...
- S Science and Technology Understanding Magnetic Storms: A Guide to This Natural Phenomenon...
- F Family and Home What is Most Important in Men s Lives?...
- G Goods and services Which TV is better - LCD or Plasma?...
- C Computers and Internet Are there special gaming mice?...
- G Goods and services LED-подсветка в LCD-телевизорах: 5 причин, почему она лучше других технологий...
- C Computers and Internet Keep Your Mouse Pad Clean: The Right Way to Clean It?...
- C Computers and Internet 3D Glasses! What is this thing?...
Answers on questions: Computers and Technology
- C Chemistry Your teacher gives you 5 minerals (diamond, gypsum, magnetite, hematite, and talc) AND a porcelain plate. What mineral property are you testing for? Explain. *...
- H History What was the mayflower compact? the ship that transported the pilgrims to america the colonial legislature of virginia the agreement in which the pilgrims agreed...
- B Business Wilbert s Clothing Stores just paid a $1.25 annual dividend. The company has a policy whereby the dividend increases by 2% annually. You would like to purchase 100...
- M Mathematics In the fig given below .AB║DC find xhelpplzzz...
Ответ:
Code is given as below:
Explanation:
def open_file():
fpointer = open('example.txt')
return fpointer
def main():
dictlist = []
dict_of_words = dict()
fp = open_file()
# loop to iterate over lines in file
for line in fp:
for word in line.split():
if(not dict_of_words.get(word)):
dict_of_words[word] = 1
else:
dict_of_words[word] += 1
for key, value in dict_of_words.items():
temp = (key.lower(), value)
dictlist.append(temp)
print(sorted(dictlist))
main()
Ответ: