Question

c) A function named tallied_data() that takes in a nested list (Use this on the data...

c) A function named tallied_data() that takes in a nested list (Use this on the data from part 1, but it should be able to be used to solve similar problems), indices for two columns and returns a tallied list.

The inputs are: i) a nested list,

ii) an index for the ‘reference column’/ ‘category’ (col_ref)

iii) another index for the column to be tallied (col_tally)

iv) this function returns a list of tuples where each element is a tuple with two elements, the first one is the reference/category and the second one is the tallied sum for all the rows that have the reference. This list will be returned sorted in descending order based on the tallied numbers. (hint: see Lesson 19)

For example using the tallied_data() function from part c: data = [[Indiana, 2, 'Clinton', 4], [‘Colorado’, 2, ‘Bush’, 56], [‘Colorado’, 2, 'Clinton', 100], [‘Illinois’, 3, ‘Gore’, 8], [Oregon, 3, 'Clinton', 4], [Indiana, 4, ‘Bush’, 2]] print(tallied_data(data, 2, 3)) will display [('Clinton', 108), ('Bush', 58), ('Gore', 8)] print(tallied_data(data, 0, 3)) will display [(‘Colorado’, 156), (‘Illinois’, 8), (‘Indiana’, 6), (‘Oregon’, 4)]

Homework Answers

Answer #1

def tallied_data(data,ref,col):
tally={}
for i in data:
if i[ref] in tally:
tally[i[ref]]+=i[col]
else:
tally[i[ref]]=i[col]
tups = []
for i in tally:
tups.append((i,tally[i]))
return sorted(tups,reverse=True,key = lambda x: x[1])
  

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python function count_bigger that takes two parameters, a nested list of objects and a...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a threshold number. It returns an integer specifying how many of the objects anywhere in the nested list are numbers that are larger than the threshold. (For our purposes, "numbers" are either integers or floats.) There may be objects in the list other than numbers, in which case you would simply ignore them. Here are a couple of examples of how the function should behave...
Write a flatten function in JavaScript that uses recursion to create an array containing all nested...
Write a flatten function in JavaScript that uses recursion to create an array containing all nested arrays in a single array. The function should pass the test: it("longer list, no mutation, completely flat", function () { var data = [[[1],2,[3]],4,[5,6]]; var orig_data = data; expect(ms.flatten(data)).toEqual([1,2,3,4,5,6]); expect(data).toEqual(orig_data); });
This code is in C++ Write a function named printMultTable that takes 2 integers, numValues and...
This code is in C++ Write a function named printMultTable that takes 2 integers, numValues and factor, and prints the first numValues greater than 0 that are multiples of  factor, separated by SPACE. It has no return value. printMultTable(5, 4) should print the first 5 multiples of 4, separated by a space, which is "4 8 12 16 20" printMultTable(3, 6) should print the first 3 multiples of 6, separated by a space, which is "6 12 18"   
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n,...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n, a positive integer, it returns a list of n tuples corresponding to the numbers 1 through n (both inclusive): the tuple for the number k consists of k as the first component, and exactly one of the following strings as the second: • the string 'two!' if k is divisible by 2 • the string 'three!' if k is divisible by 3 • the...
create a function that takes a dictionary and returns a list of int. The list should...
create a function that takes a dictionary and returns a list of int. The list should appear in decreasing order based on the sum of number in each dictionary. def total_num(dict1): #Code here input = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':3}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} output = [1,5,3,4,2] 1: 38 2: 5 3: 8 4: 6 5: 15 1>5>3>4>2
Define a function in Scheme which takes a list of numbers and a scalar and returns...
Define a function in Scheme which takes a list of numbers and a scalar and returns the rest of the list after the first occurrence of the scalar that was passed. An empty list should be returned if the value doesn't exist. For example, (list-past-scalar '(5 2 8 3 1 9 2 3) 3) #returns (1 9 2 3) (list-past-scalar '(37 18 38 65 90) 100) #returns ()
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a...
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a list of integers representing how many integers in each sublist of the original were divisible by 5. Examples: >>> count_div5([[5, 3, 25, 4], [46, 7], [5, 10, 15]]) [2, 0, 3] >>> count_div5([]) [] >>> count_div5([[-20, 10, 2, 4, 5], [], [5], [8, 25, 10], [6]]) [3, 0, 1, 2, 0]
Using C programming Create a function called printMenu( ) with the following properties: Has no function...
Using C programming Create a function called printMenu( ) with the following properties: Has no function inputs or output. Prints the following menu to the screen: 1. Enter user name. 2. Enter scores. 3. Display average score. 4. Display summary. 5. Quit Create a function called printLine( ) with the following properties: Takes as input a char Takes as input an integer corresponding to the number of times to print the character Has no function output. For example, if we...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...