Experiment with Python dictionaries and their keys () member function that returns a list of a...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Experiment with Python dictionaries and their keys () member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is MyDict = { 1: 'One', 2: 'Two', 3: 'Three' } Then ( 2 in MyDict.keys() ) evaluates to True, indicating that 2 is a valid key for MyDict. This can be used in conditional statements (if statements, while statements, etc.). 1. Data stored in a dictionary for easy lookup Some data files exists such that the layout of the data in them is consistent. The files have a first row of headers and the remaining rows of data. The first column of data has no header, but is always the data row number, increasing by 1 for each row, starting at O. An example (available on CatCourses as HW05_01_data_1.txt) is below. 0 1 2 3456 6 LABEL default Potion Scroll Treasure Body Bones Pouch Name 6671 66483 66484 66490 66492 66493 66494 Appearance **** 194 195 196 198 199 200 Look over the data file. You will notice that the columns are separated by spaces and no data or header has a space inside. There will be other data files, but they will follow the same format, even if they have different headers, numbers of columns, numbers of rows, and spacing between columns. Some other data files with the same layout are given in CatCourses as HW05_01_data_2.txt, HW05_01_data 3. txt, and HW05_01_data_4.txt. Write a script that has a function that is given a data file name as a string and reads such a data file. As always, do this using built-in Python function, not using any imported data reading modules. The function will return a dictionary with the headers as keys and the data for the corresponding column in a list as that key's values. The data for the first column of row number data does not need to be stored. That values can remain as strings. The function should be called GetDataDict. An example of its use in PythonWin is below. >>> import os >>> os.chdir (r'c:\Users\brokow\UCM Classes\ME 021\2023 Spring Assignments') >>> HW05_01. GetDataDict('HW05_01_data_1.txt') {"LABEL": ['default', 'Potion', 'scroll', 'Treasure', 'Body', 'Bones', 'Pouch'], 'Name': ['6671', ¹66483', '66484', '66490', '66492', '66493', '66494'1, 'Appearance': ['****', '194', '195¹, '196', '198', '199', '200¹]} >>> Experiment with Python dictionaries and their keys () member function that returns a list of a dictionary's keys. You can check if a particular key is in a dictionary by using the in test with the keys list. For example, if our dictionary is MyDict = { 1: 'One', 2: 'Two', 3: 'Three' } Then ( 2 in MyDict.keys() ) evaluates to True, indicating that 2 is a valid key for MyDict. This can be used in conditional statements (if statements, while statements, etc.). 1. Data stored in a dictionary for easy lookup Some data files exists such that the layout of the data in them is consistent. The files have a first row of headers and the remaining rows of data. The first column of data has no header, but is always the data row number, increasing by 1 for each row, starting at O. An example (available on CatCourses as HW05_01_data_1.txt) is below. 0 1 2 3456 6 LABEL default Potion Scroll Treasure Body Bones Pouch Name 6671 66483 66484 66490 66492 66493 66494 Appearance **** 194 195 196 198 199 200 Look over the data file. You will notice that the columns are separated by spaces and no data or header has a space inside. There will be other data files, but they will follow the same format, even if they have different headers, numbers of columns, numbers of rows, and spacing between columns. Some other data files with the same layout are given in CatCourses as HW05_01_data_2.txt, HW05_01_data 3. txt, and HW05_01_data_4.txt. Write a script that has a function that is given a data file name as a string and reads such a data file. As always, do this using built-in Python function, not using any imported data reading modules. The function will return a dictionary with the headers as keys and the data for the corresponding column in a list as that key's values. The data for the first column of row number data does not need to be stored. That values can remain as strings. The function should be called GetDataDict. An example of its use in PythonWin is below. >>> import os >>> os.chdir (r'c:\Users\brokow\UCM Classes\ME 021\2023 Spring Assignments') >>> HW05_01. GetDataDict('HW05_01_data_1.txt') {"LABEL": ['default', 'Potion', 'scroll', 'Treasure', 'Body', 'Bones', 'Pouch'], 'Name': ['6671', ¹66483', '66484', '66490', '66492', '66493', '66494'1, 'Appearance': ['****', '194', '195¹, '196', '198', '199', '200¹]} >>>
Expert Answer:
Answer rating: 100% (QA)
import collections dicton ny whilenn ifn0 break else k rawinputenter name i... View the full answer
Related Book For
Concepts of Database Management
ISBN: 978-1285427102
8th edition
Authors: Philip J. Pratt, Mary Z. Last
Posted Date:
Students also viewed these programming questions
-
Sue contracts to buy six cases of vintage Barrona Valley wine from Goodvines Winery for $1,200. The contract states that delivery is to be made at Sue's residence "on or before May 1, to be used for...
-
Write a Python program that, in a loop, asks the user for a name and then a numerical score, and then asks whether to do another. The user may enter the same name more than one time. When a name is...
-
List three specific parts of the Case Guide, Objectives and Strategy Section (See below) that you had the most difficulty understanding. Describe your current understanding of these parts. Provide...
-
Let a 0. Solve |x| = 3.
-
In the pivoted motor mount shown, the weight W of the 85-kg motor is used to maintain tension in the drive belt. Knowing that the coefficient of static friction between the flat belt and drums A and...
-
The woman A, the captain B, and the sailor C weigh 120, 180, and 160 lb, respectively, and are sitting in the 300-lb skiff, which is gliding through the water with a speed of 1 knot. If the three...
-
Consider the mean square error criterion for generalized ridge regression. Show that the mean square error is minimized by choosing \(k_{j}=\sigma^{2} / \alpha_{j}^{2}, j=1\), \(2, \ldots, p\).
-
Flint Fabricators Inc. machines metal parts for the automotive industry. Under the traditional manufacturing approach, the parts are machined through two processes: milling and finishing. Parts are...
-
In what ways do virtue ethics challenge traditional consequentialist and deontological frameworks in moral philosophy ?
-
Dana Rand owns a catering company that prepares banquets and parties for both individual and business functions throughout the year. Rand's business is seasonal, with a heavy schedule during the...
-
1. Calculate the budgeted nights booked: Maximum capacity (30 rooms) * Number of days per year (365) * Expected occupancy rate (80%) = 8760 nights. 2. Calculate the tariff revenues: Budgeted nights...
-
Customers arrive at a rate of 15 per hour and it takes an average of 2 minutes to provide the service to the customer. There is only one server. Interarrival times and service times are exponential....
-
1.) What is meant by "diminishing marginal utility"? 2.) What are some things that you have/get where you experience diminishing marginal utility? Relatedly, what is happening to your total utility?
-
What would be the output of the following? [ ] x = 5 XNN X * 2 Y-3 A/
-
What is the output of the following code: [] # This is the input y="data science for all" y[6:9]
-
Mr. Smith is a 75 year old retiree who lives alone One day, he receives a phone call threatening him that his power will be cut off it he doesn't purchase a gift card, call back and use the gift card...
-
Q4. You have filled bath tub with very hot water. Rather than risk 3rd degree burns, you decide to wait for it to cool down. Whilst waiting, you start to wonder about how the changing temperature of...
-
Write the general quadratic equation y2 - 8y - 4x + 28 = 0 in standard form. Determine the vertex, focus, and directrix of the parabola defined by this equation. Sketch a graph.
-
What is a domain?
-
What is an OODBMS?
-
How do classes relate to objects?
-
A guitar player can change the frequency of a string by bending itpushing it along a fret that is perpendicular to its length. This stretches the string, increasing its tension and its frequency. The...
-
The future of work may look very different from how it does today. The economy has had a significant influence on OB in recent years. The gig economy (also known as freelancing) has arisen as a major...
-
Creativity is affectively charged, and complex cognitive processes are shaped by both emotions and moods. Creativity may be particularly susceptible to affective influence, mainly because positive...
Study smarter with the SolutionInn App