Write a function that, given a 2D list, creates and returns a dictionary that has one...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a function that, given a 2D list, creates and returns a dictionary that has one key/value pair for each sublist. The key is the first element from the sublist (as a single element), and the value is the rest of the sublist (as a list). You can assume that the first element in each row is distinct, so you don't need to worry about overwriting keys. Here are the specs: Function name: Ist_to_det Parameters: a 2D list of any data type, of the form [[x, y, z], [x, y, z]...] Returns: a dictionary where the key = x and value = [y, z] for every row in the 2D list Examples of calling your function: • 1st_to_dct ( [[1, 2, 3], [4, 5, 6]]) 1st_to_dct ( [[10, 11], [12, 13]]) Problem 2 (2D List to Dictionary V2) Might come in handy... returns (1 returns (10 [2, 3], 4 [5, 6]} [11], 12 [13]} Iterating by position. Normally to iterate by position over a 2d list, we would do.. for i in range (len (1st)): for j in range (len (1st[i])): # 1st[i][j] is the element at row i, column j Problem 2 (2D List to Dictionary V2) Might come in handy... ● Iterating by position. Normally to iterate by position over a 2d list, we would do.. for i in range (len (1st)): In this problem, we're swapping rows and columns so you probably won't want to use that exact setup, but the idea might be useful. for j in range (len (1st[i])): Write a function that, given a 2D list which includes a header, creates and returns a dictionary keyed on the header. name # 1st[i][j] is the element at row i, column j Imagine a 2d list representing a CSV file with a header, like this: attribute Grizz Carol happy hugs fault "attribute" : ["happy", "hugs"], "fault" ["barking", "jumping"]} barking jealous We want it to turn into a single dictionary where the keys come from the header, and the values are lists. {"name": ["Grizz", "Carol"], You can assume all rows, including the header row at position 0, are the same length. You can assume all rows, including the header row at position 0, are the same length. Function name: Ist_to_dct_v2 ● Parameters: a 2D list of any data type, including a header row at the beginning ● ● Returns: a dictionary keyed on the header row; each value is a list containing the elements of the column under the header Examples of calling your function: ● 1st_to_dct_v2([["h1", "h2"], [1, 2]]) returns {"h1" : [1], "h2" : [2]} ● 1st_to_dct_v2([["h1", "h2"], [1, 2], [3, 4]]) returns {"h1" : [1, 3], "h2" [2, 4]} Problem 3 (2D List to List of Dictionaries) Might come in handy... In this case the header will be at 1st [0], and the main data will be at 1st [1:] Because we're making a list of dictionaries, you should make a new list (newlst least once, and a new dictionary (dct = {}) many times. = []) at Write a function that, given a 2D list which includes a header, creates and returns a list dictionaries. All the dictionaries have the same keys, which come from the header. Imagine a 2d list representing a CSV file with a header, like this: name Grizz Carol attribute happy hugs ● fault We want it to turn into a list of two dictionaries, each with three key/value pairs, like this: [{"name": "Grizz", "attribute" : "happy", "fault": "barking"}, {"name": "Carol", "attribute" : "hugs", "fault": "jealous"} ] barking jealous You can assume the given list is not empty, and that every sublist is the same length. Here are the specs: ● Function name: Ist_to_dct_lst ● Parameters: a 2D list of any data type, of the form [[h1, h2, h3], [x, y, z], [x, y, z]...] ● Returns: a list of dictionaries where the keys all come from the header row and values from each sublist Problem 4 (Max in a Dictionary of Lists) Examples of calling your function: ● 1st_to_dct_1st([[h1, h2], [1, 2], [4, 5]])..... returns[ {h1 : 1, h2: 2}, {h1 : 4, h2, 5} ] Might come in handy... ● Python has a built-in max function that can accept a list. However if you give max a dictionary, it will find the maximum key, not maximum value. You can use list (dct.keys()) or list (dct.values()) to turn the keys or values of a dictionary into an actual list data type Pythons 1st.index(value) method returns the position of the first occurrence of value in the list lst. Write a function that takes in a dictionary like you would have produced in Problem 1. This dictionary has all of its values as lists, and we want to find the largest element at a given position among all of them. For example, if the given position is 1, then look at all the position 1s in all of the lists, and return the key where the element at position 1 is the largest. Here are the specs: ● Function name: find_max ● Parameters: a dictionary where every value is a list, and an integer for a position ● Returns: the key in the given dictionary, where its value at a given position is the highest Examples of calling your function: find_max({"a" : [1, 5], "b" : [4, 3]}, 0) find_max({"a" : [1, 5], "b" : [4, 3]}, 1) Problem 5 (List of Sums) Might come in handy... List comprehension. This is a quick way to make a new list with 10x the value from an original 1st.... newlst = [num * 10 for num in 1st] Remember that it's totally fine to write small helper functions, or to call functions you've already written. Write a function that takes in a 2d list. You can assume every element in the list is a number. Your function should create and return a list of numbers that represents the sum of each column. Here are the specs: returns "b" returns "a" ● Function name: sum_cols Parameters: a 2d list of numbers ● Returns: a list of numbers, the sum of each column Write a function that, given a 2D list, creates and returns a dictionary that has one key/value pair for each sublist. The key is the first element from the sublist (as a single element), and the value is the rest of the sublist (as a list). You can assume that the first element in each row is distinct, so you don't need to worry about overwriting keys. Here are the specs: Function name: Ist_to_det Parameters: a 2D list of any data type, of the form [[x, y, z], [x, y, z]...] Returns: a dictionary where the key = x and value = [y, z] for every row in the 2D list Examples of calling your function: • 1st_to_dct ( [[1, 2, 3], [4, 5, 6]]) 1st_to_dct ( [[10, 11], [12, 13]]) Problem 2 (2D List to Dictionary V2) Might come in handy... returns (1 returns (10 [2, 3], 4 [5, 6]} [11], 12 [13]} Iterating by position. Normally to iterate by position over a 2d list, we would do.. for i in range (len (1st)): for j in range (len (1st[i])): # 1st[i][j] is the element at row i, column j Problem 2 (2D List to Dictionary V2) Might come in handy... ● Iterating by position. Normally to iterate by position over a 2d list, we would do.. for i in range (len (1st)): In this problem, we're swapping rows and columns so you probably won't want to use that exact setup, but the idea might be useful. for j in range (len (1st[i])): Write a function that, given a 2D list which includes a header, creates and returns a dictionary keyed on the header. name # 1st[i][j] is the element at row i, column j Imagine a 2d list representing a CSV file with a header, like this: attribute Grizz Carol happy hugs fault "attribute" : ["happy", "hugs"], "fault" ["barking", "jumping"]} barking jealous We want it to turn into a single dictionary where the keys come from the header, and the values are lists. {"name": ["Grizz", "Carol"], You can assume all rows, including the header row at position 0, are the same length. You can assume all rows, including the header row at position 0, are the same length. Function name: Ist_to_dct_v2 ● Parameters: a 2D list of any data type, including a header row at the beginning ● ● Returns: a dictionary keyed on the header row; each value is a list containing the elements of the column under the header Examples of calling your function: ● 1st_to_dct_v2([["h1", "h2"], [1, 2]]) returns {"h1" : [1], "h2" : [2]} ● 1st_to_dct_v2([["h1", "h2"], [1, 2], [3, 4]]) returns {"h1" : [1, 3], "h2" [2, 4]} Problem 3 (2D List to List of Dictionaries) Might come in handy... In this case the header will be at 1st [0], and the main data will be at 1st [1:] Because we're making a list of dictionaries, you should make a new list (newlst least once, and a new dictionary (dct = {}) many times. = []) at Write a function that, given a 2D list which includes a header, creates and returns a list dictionaries. All the dictionaries have the same keys, which come from the header. Imagine a 2d list representing a CSV file with a header, like this: name Grizz Carol attribute happy hugs ● fault We want it to turn into a list of two dictionaries, each with three key/value pairs, like this: [{"name": "Grizz", "attribute" : "happy", "fault": "barking"}, {"name": "Carol", "attribute" : "hugs", "fault": "jealous"} ] barking jealous You can assume the given list is not empty, and that every sublist is the same length. Here are the specs: ● Function name: Ist_to_dct_lst ● Parameters: a 2D list of any data type, of the form [[h1, h2, h3], [x, y, z], [x, y, z]...] ● Returns: a list of dictionaries where the keys all come from the header row and values from each sublist Problem 4 (Max in a Dictionary of Lists) Examples of calling your function: ● 1st_to_dct_1st([[h1, h2], [1, 2], [4, 5]])..... returns[ {h1 : 1, h2: 2}, {h1 : 4, h2, 5} ] Might come in handy... ● Python has a built-in max function that can accept a list. However if you give max a dictionary, it will find the maximum key, not maximum value. You can use list (dct.keys()) or list (dct.values()) to turn the keys or values of a dictionary into an actual list data type Pythons 1st.index(value) method returns the position of the first occurrence of value in the list lst. Write a function that takes in a dictionary like you would have produced in Problem 1. This dictionary has all of its values as lists, and we want to find the largest element at a given position among all of them. For example, if the given position is 1, then look at all the position 1s in all of the lists, and return the key where the element at position 1 is the largest. Here are the specs: ● Function name: find_max ● Parameters: a dictionary where every value is a list, and an integer for a position ● Returns: the key in the given dictionary, where its value at a given position is the highest Examples of calling your function: find_max({"a" : [1, 5], "b" : [4, 3]}, 0) find_max({"a" : [1, 5], "b" : [4, 3]}, 1) Problem 5 (List of Sums) Might come in handy... List comprehension. This is a quick way to make a new list with 10x the value from an original 1st.... newlst = [num * 10 for num in 1st] Remember that it's totally fine to write small helper functions, or to call functions you've already written. Write a function that takes in a 2d list. You can assume every element in the list is a number. Your function should create and return a list of numbers that represents the sum of each column. Here are the specs: returns "b" returns "a" ● Function name: sum_cols Parameters: a 2d list of numbers ● Returns: a list of numbers, the sum of each column
Expert Answer:
Answer rating: 100% (QA)
Certainly Ill provide Python code for each of the problems you described Problem 1 2D List to Dictio... View the full answer
Related Book For
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest
Posted Date:
Students also viewed these programming questions
-
Post the entries in the general journal below to the accounts payable account in the general ledger and to the appropriate accounts in the accounts payable ledger. Assume the following account...
-
What will the following code print out? int x-7; int one &x; x-14; int two-&x; two 3; cout < < "one < < *two;
-
For each of the following exercises, write the Java code statements to perform the action(s) requested. Submit your code statements and a capture of your outputs for each exercise. 1. Take 5 integers...
-
Suppose that the Medicare rate of hospital reimbursement is reduced. Explain why the costs may not be shifted to other patients in the short run.
-
Find the final values of the time function f (t) if F(s) is given as F(s) = 10(s + 1) / (s + 2) (s + 3) F(s) = 10 / s2 + 4s + 4
-
As a result of its annual inventory count, Tarweed Corp. determined its ending inventory at cost and at lower of cost and net realizable value at December 31, 2016, and December 31, 2017. This...
-
An airplane is flying at a flight (or local) Mach number of 0.70 at \(10,000 \mathrm{~m}\) in the Standard Atmosphere. Find the ground speed (a) if the air is not moving relative to the ground (b) if...
-
Sparrow Corporation (a calendar year, accrual basis taxpayer) had the following transactions in 2015, its second year of operation. Taxable income .. $330,000 Federal income tax liability paid ...
-
Managers often use variance analysis in employee performance evaluations for the following two attributeseffectiveness and efficiency. Variance analysis alerts management to whether the organization...
-
2. Consulting: A University employs a Professor whose base output is a constant K. The Professor chooses to spend effort e consulting, giving her income pe at personal cost C(e) e/2. Unfortunately,...
-
Based on the Common-Size Balance Sheet, does Company A or Company B take on more risk in how they finance their assets?
-
determine eigen vectors for eigen values 31. 1 2 3 4 4321 4567 7654
-
3. For each of the following, calculate the cost of goods sold and the gross profit. Sales Beginning Inventory Purchases Ending Inventory 1. $125 000 32 000 74 250 33 500 2345 2. $750 585 85 600 410...
-
What is your estimate of the total charge, in dollars, for concrete transportation for this project? Your answer should be rounded to the nearest 1000. Do not enter a $ sign. Hint: There are 1000...
-
The simplified trial balance for Viera Associates at December 31, 20-, after a fiscal period of one year, is given below, along with some additional information. Viera Associates ACCOUNTS Bank...
-
You buy a 10-year $1,000 par value 3.80% annual-payment coupon bond priced to yield 5.80%. You do not sell the bond at year-end. If you are in a 15% tax bracket, at year-end you will owe taxes on...
-
Imagine that you are travelling in pre-COVID times. You leave Vancouver with $20,000, and travel to Britain and exchange your Dollars for British Pounds (BP). After spending 2 weeks in Britain you...
-
Write the expression in radical notation. Then evaluate the expression when the result is an integer. 23 -1/2
-
Consider the searching problem: Input: A sequence of n numbers A = a 1 , a 2 ,......,a n and a value . Output: An index i such that = A[i] or the special value NIL if does not appear in A. Write...
-
There are two types of professional wrestlers: "babyfaces" ("good guys") and "heels" ("bad guys"). Between any pair of professional wrestlers, there may or may not be a rivalry. Suppose we have n...
-
Approximate n k = 1 k 3 with an integral.
-
FIFO and average cost flow methods periodic and perpetual inventory systems Non-GST version Clean Air Ltd sells air purifiers. The table below summarises the inventory movements for the most popular...
-
Personal Tech Ltd records its inventory of fitness watches using the FIFO cost allocation for perpetual inventory. The following information summarises the inventory movements for their most popular...
-
Audio Attitude Ltd uses the perpetual inventory system to maintain its inventory records of Play it car stereos. The computerised accounting system identified the closing inventory value was \($245\)...
Study smarter with the SolutionInn App