Question

In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as...

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]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# Open file in read mode
words_dictionary = open("infile.txt", "r")

# Empty dictionary
word_list = {4: [], 5: [], 6: []}

# Printing initial message
print("Dictionary empty at initialization")
print(word_list)

def main():
   # Iterate line by line in file
   for line in words_dictionary:
       line = line.strip() # Strippng lines
       word_length = len(line) # Length of word
       # appending value to respective keys in dict based on word length
       if(word_length == 4):
           word_list[4].append(line)
       if(word_length == 5):
           word_list[5].append(line)
       if(word_length == 6):
           word_list[6].append(line)

main()
print(word_list)

Input file

OUTPUT

Add a comment
Know the answer?
Add Answer to:
In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • I am having trouble with my Python code in this dictionary and pickle program. Here is...

    I am having trouble with my Python code in this dictionary and pickle program. Here is my code but it is not running as i am receiving synthax error on the second line. class Student: def__init__(self,id,name,midterm,final): self.id=id self.name=name self.midterm=midterm self.final=final def calculate_grade(self): self.avg=(self.midterm+self.final)/2 if self.avg>=60 and self.avg<=80: self.g='A' elif self.avg>80 and self.avg<=100: self.g='A+' elif self.avg<60 and self.avg>=40: self.g='B' else: self.g='C' def getdata(self): return self.id,self.name.self.midterm,self.final,self.g CIT101 = {} CIT101["123"] = Student("123", "smith, john", 78, 86) CIT101["124"] = Student("124", "tom, alter", 50,...

  • Python Problem Hello i am trying to finish this code it does not save guesses or...

    Python Problem Hello i am trying to finish this code it does not save guesses or count wrong guesses. When printing hman it needs to only show _ and letters together. For example if the word is amazing and guess=a it must print a_a_ _ _ _ not a_ _ _ _ and _a_ _ _ separately or with spaces. The guess has a maximum of 8 wrong guesses so for every wrong guess the game must count it if...

  • I am working on the divide/conquer algorithm. I am having a trouble with a print for...

    I am working on the divide/conquer algorithm. I am having a trouble with a print for output from reading the file. Here is my work. When you see I put the comment with TODO. that one I am struck with readfile. I wonder if you'd able to help me to fix the readfile to find a single number. Here is the input3.txt (1 12 13 24 35 46 57 58 69). after that, the output should be 0. int mergeInversion(int...

  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • Below is what I am working. The first two screen caps are from Codio a program we use online at my school. I have been r...

    Below is what I am working. The first two screen caps are from Codio a program we use online at my school. I have been running my code through Python 3 to make sure it works before submitting my assignments; usually I have little to no issues. But now I am stuck on this one problem. Please help. Step 4: Construct confidence interval for population proportion Construct a 99% confidence interval for the proportion of months with rice production above...

  • I need help with this code, I'm stuck on it, please remember step 4, I'm very...

    I need help with this code, I'm stuck on it, please remember step 4, I'm very much stuck on that part. It says something about putting how many times it appears Assignment #1: Sorting with Binary Search Tree Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. 1 Perform basic file I/O. 2. Use structs, pointers, and strings. Use dynamic memory. 3. 4. This assignment asks you to sort the...

  • Using Java; Consider the following text file format. Ruminant Shaman 30 Elentriel Shaman 70 Aelkine Druid...

    Using Java; Consider the following text file format. Ruminant Shaman 30 Elentriel Shaman 70 Aelkine Druid 29 Barbatus Druid 70 Ukaimar Mage 47 Mariko Priest 33 Farmbuyer Priest 70 Valefar Warlock 42 Teslar Paladin 64 Nerdsbane Hunter 12 We wish to store information regarding several characters in a video game. Each character has a name, and a class, and a level. This information is repeated, 3 lines per character, for any number of characters. To simplify processing, the very first...

  • I need help in C++ assignment. please add statements and i am Xcode complier user. Requested...

    I need help in C++ assignment. please add statements and i am Xcode complier user. Requested files: CountDecades.cpp (Download) Maximum upload file size: 96 KiB Write a C++ program named CountDecades.cpp. In this program you will have two integer arrays. One named inputArray of size 20 containing the values: 83, 2, 23, 11, 97, 23, 41, 67, 16, 25, 1 , 4, 75, 92, 52, 6, 44, 81, 8, 64 in the order specified, and an empty integer array of...

  • I am trying to do this assignment: Write a program that reads a list of concepts/strings...

    I am trying to do this assignment: Write a program that reads a list of concepts/strings from the LIST.txt text file (one concept per line), stores them into an array of strings, and then prints the following: 1.Prints a heading “LIST1:” and then prints all the concepts from the array, one concept per line 2.Prints a heading “LIST2:” and then prints all the concepts from the array that do not contain spaces, one concept per line 3.Prints a heading “LIST3:”...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT