1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
svetoff [14.1K]
3 years ago
11

Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a fil

e. You will need to decide on the following: How to format each dictionary item as a text string in the input file. How to covert each input string into a dictionary item. How to format each item of your inverted dictionary as a text string in the output file. Create an input file with your original three-or-more items and add at least three new items, for a total of at least six items.
Computers and Technology
1 answer:
Bond [772]3 years ago
6 0

Answer:

Following are the code to this question:

import ast as a #import package ast

d1= {}#defining an empty dictionary  

def invert_dictionary(d1):#defining a method invert_dictionary

   i = dict()#defining variable i that hold dict method  

   for k in d1:#defining for loo to check dictionary value

       val = d1[k]#defining val variable to holds dictionary key values

       for val1 in val:#defining another for loop to hold value of dictionary and inverse it

           if val1 not in i:#defining if block to check value

               i[val1] = [k]#hold key value

           else:#defining else block

               i[val1].append(k)#add value in dictionary

   return i#return inverse value

with open("dict_items.txt", "r") as data:#use open method to add a list as a file

   dict_item = a.literal_eval(data.read())#defining a variable that covert item as a text string in the input file  

   '''covert input string into dictionary item'''

   e_val = input("Please enter a value")#defining variable e_val to input value

   dict_item['4']=str(e_val)#holding value in dictionary

   dict_item['5']='e'#holding value in dictionary

   dict_item['6']='f'#holding value in dictionary

   '''Invert the dictionary value '''

   invert_data =invert_dictionary(dict_item)#defining variable to holding method value

'''calculating the format of each item of inverted dictionary as a text string output file.'''

f_data = {i:str(j[0]) for i,j in inverted_data.items() }#defining f_data to hold coverted value

import json as j#import package

with open('out_put.txt', 'w') as file:#use open method to open file

   file.write(j.dumps(f_data))#add value

Explanation:

In the above-given code has four parts which can be defined as follows:

In the first part, a method invert_dictionary is defined that accepts a dictionary, and defines two for loops in which the second loop uses a conditional statement to convert the inverse form and return its value, and after inverse it use the open method to add a list as a file, and defining a variable that covert item as a text string in the input file.

In the second step, a variable e_val is declared for input value , and use the dict_item variable to holding dictionary  value, and call the method and hold its value.

In the third and fourth step it calculating the format of each item of inverted dictionary as a text string output file.

You might be interested in
How do you give the brainliest answer to someone?
Hatshy [7]
You click where the stars are and it says brainliest answer.
4 0
3 years ago
Read 2 more answers
Which tab allows you to review the slideshow timings?<br> O Home<br> O View<br> O Review<br> O File
disa [49]

Answer:

view

Explanation:

4 0
3 years ago
Why would a brokered CD pay more than a regular CD?
Lana71 [14]

Brokered CDs may have higher or lower rates than those purchased directly from bans and credit unions. The general consensus here is that higher rates are usually available for direct purchases.

3 0
3 years ago
Which of the following statements about personality tests are true? Check all of the boxes that apply.
borishaifa [10]

Answer:

They evaluate a person’s personality traits And They can be used as self-assessment tools for career planning. are the answers.

4 0
4 years ago
Background Susan finished work on system architecture issues, and her system design specification was approved. Now she is ready
beks73 [17]

Answer:

i think it can be

Explanation:

4 0
4 years ago
Other questions:
  • To remove any hidden days from your document before sharing it, what should you do?
    7·1 answer
  • Which term defines a star system with two stars?
    13·2 answers
  • What should you keep in mind when typing a message on the Internet?use as many A.acronyms as it saves space
    7·1 answer
  • You can create user accounts on the linux system by using the ____ command.
    9·1 answer
  • List several things that geographers can map using remotely sensed data
    5·1 answer
  • I NEED HELP PLEASE ITS TIMED
    9·1 answer
  • One lap around a standard high-school running track is exactly 0.25 miles. Write a program that takes a number of miles as input
    12·2 answers
  • A garments manufacturing company buys various types of natural and synthetic materials to produce clothes. Which material is a s
    6·2 answers
  • &gt;What is the output of the following code:
    5·1 answer
  • Anyone know how to fix black screen of death on computer​
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!