Question: In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as keys and an array for key containing a list

In python,

PART A: I am trying to get a dictionary with size(4, 5, 6) as keys and an array for key containing a list of values (words from file of respective size) associated with those keys

I am reading from a file of strings, where I am only interested with words of length 4, 5, and 6 to compute my program reading the text file line by line:

At first, I have an empty dictionary then to that I want to add values to my arrays based on their respective size of strings

words_dictionary = open('file') word_list = dict([(4, []), (5, []), (6, [])]) print("Dictionary empty at initialization") print(word_list) def main(): lines = words_dictionary.readline().strip() count = 1
 for line in words_dictionary:  //I want to use a for loop, but I don't know how to go about that word_length = len(lines) if (word_length >= 4) and (word_length <= 6): lines = words_dictionary.readline().strip() words_dictionary[0] = words_dictionary[line[0]].append(line[1]) //Here is where I am trying to add values to the arrays words_dictionary[1] = words_dictionary[line[0]].append(line[1]) words_dictionary[2] = words_dictionary[line[0]].append(line[1]) print("Line {}: {} {} ".format(count, words_dictionary, word_length)) lines = words_dictionary.readline().strip() count += 1

main()

input file: { backspace, scan, power, vegan,delete, hope, meeting, limits, videos, phone, love, envy}

Example output:

4: [love, hope, scan, envy]

5 : [phone , power, vegan]

6:[delete, limits, videos]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!