Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python! Write a function called get_file_index(filename) that takes the name of a file as a parameter and returns a dictionary in which the keys are
Python!
Write a function called get_file_index(filename) that takes the name of a file as a parameter and returns a dictionary in which the keys are all the words found in the file and the values are the line numbers (in ascending order) at which the words were found in the file. Since words may appear in multiple lines and multiple times in the same line, the values are all lists. You may assume the input file exists, that it contains at least one line, and that input lines are either empty or contain only space separated words. If there are no words in the input file, an empty dictionary should be returned. For example, if the input file were this is a file this is line two line another line the resulting dictionary would be (with arbitrary key ordering): {'is': [1, 2], 'line': [2, 3, 3], 'another': [3], 'two': [2], 'a': [1], 'this': [1, 2], 'file': [1]} Note: the lines are indexed from 1 and a word appearing multiple times on the same line should have that line number added to its list multiple times. For example: For example: Test Result dict1 = get_file_index('basic.txt') for key in sorted(dicti.keys(): print(key, dicti[key]) a [1] another [3] file [1] is [1, 2] line [2, 3, 3] this [1, 2] two [2] dict1 = get_file_index('data.txt') for key in sorted (dicti.keys(): print(key, dict1[key]) C5101 [2, 4] Everyone [4] I'm [2] It's [3] computer [1] do [4] enjoying [1] enrolled [2] fun [3] great [3] i'm [1] in [2] science [1] should [4] this [2] year [2]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started