Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this lab, we re organizing data into different structures so we can ask and answer different questions about it . Often, when we read

In this lab, were organizing data into different structures so we can ask and answer different questions about it. Often, when we read data in from a CSV file, we start with a 2D list that mirrors the file. So well start with that today, and reorganize the data a few different ways.
We have might come in handy... hints for each problem. You can feel free to ignore these, they are just there as potentially helpful hints depending on how you approach the problem. Weve put them first under each problem, but you might want to come back to these hints after youve read the rest of the problem and maybe started your solution.
Problem 1(2D List to Dictionary)
Might come in handy...
Handout: Python Dictionaries
List slicing. Use lst[1:] to get everything except position 0 in the list lst.
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 dont need to worry about overwriting keys. Here are the specs:
Function name: lst_to_dct
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:
lst_to_dct([[1,2,3],[4,5,6]])..... returns {1 : [2,3],4 : [5,6]}
lst_to_dct([[10,11],[12,13]])..... returns {10 : [11],12 : [13]}
Problem 2(Column to List)
Might come in handy...
Handout: Lists and Loops
Write a function that, given a 2D list and a column number, returns all the values from that column as a list. You can assume that a valid column number was given to your function.
Function name: col_to_lst
Parameters: a 2D list of any data type, integer for a column number
Returns: a list of the same data type, containing all values from one column
Examples of calling your function:
col_to_lst([[1,2],[3,4]],0)....returns [1,3]
col_to_lst([[1,2],[3,4],[5,6]],1)....returns [2,4,6]
Problem 3(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 lst.... newlst =[num *10 for num in lst]
Remember that its totally fine to write small helper functions, or to call functions youve 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:
Function name: sum_cols
Parameters: a 2d list of numbers
Returns: a list of numbers, the sum of each column
Examples of calling your function:
sum_cols([[10,20],[1,1]])..... returns [11,21]
sum_cols([[1,2,3],[4,5,6]])..... returns [5,7,9]
Problem 4(Max in a Dictionary of Lists)
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 lst.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)..... returns "b"
find_max({"a" : [1,5],"b" : [4,3]},1)..... returns "a"
Problem 5(2D List to List of Dictionaries)
Might come in handy...
In this case the header will be at lst[0], and the main data will be at lst[1:]
Because were making a list of dictionaries, you should make a new list (newlst =[]) at least once, and a new dictionary (dct ={}) many times.
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
attribute
fault
Grizz
happy
barking
Carol
hugs
jealous
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"}]
You can assume the given list is not empty, and that ever

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

Answer the questions in the table below about this molecule

Answered: 1 week ago