Answered step by step
Verified Expert Solution
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 D 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 D List to Dictionary
Might come in handy...
Handout: Python Dictionaries
List slicing. Use lst: to get everything except position in the list lst
Write a function that, given a D list, creates and returns a dictionary that has one keyvalue 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: lsttodct
Parameters: a D list of any data type, of the form x y zx y z
Returns: a dictionary where the key x and value y z for every row in the D list
Examples of calling your function:
lsttodct returns : :
lsttodct returns : :
Problem Column to List
Might come in handy...
Handout: Lists and Loops
Write a function that, given a D 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: coltolst
Parameters: a D 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:
coltolstreturns
coltolstreturns
Problem List of Sums
Might come in handy...
List comprehension. This is a quick way to make a new list with x the value from an original lst newlst num 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 d 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: sumcols
Parameters: a d list of numbers
Returns: a list of numbers, the sum of each column
Examples of calling your function:
sumcols returns
sumcols returns
Problem Max in a Dictionary of Lists
Might come in handy...
Python has a builtin 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 listdctkeys or listdctvalues to turn the keys or values of a dictionary into an actual list data type
Pythons lstindexvalue 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 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 then look at all the position s in all of the lists, and return the key where the element at position is the largest.
Here are the specs:
Function name: findmax
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:
findmaxa : b : returns b
findmaxa : b : returns a
Problem D List to List of Dictionaries
Might come in handy...
In this case the header will be at lst and the main data will be at lst:
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 D 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 d 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 keyvalue 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started