import csv
with open('candy-data.csv', "r") as f:
reader = csv.reader(f)
candy_data = list(reader)
candy_data.pop(0)
candy_data_sparse = list(map(lambda x: [x[0], float(x[12])], candy_data))
candy_data_sorted = sorted(candy_data_sparse, reverse=True, key=lambda x: x[1])
name_with_max = candy_data_sorted[0]
name_of_current_max = "none"
current_max = 0
for row in candy_data:
if float(row[12]) > current_max:
current_max = float(row[12])
name_of_current_max = row[0]
f1 = open('result-1.csv', "w")
f1.write(name_with_max[0])
names_with_choc = []
for line in candy_data:
if int(line[1]) is 1:
names_with_choc.append(line[0])
result_string = ""
for name_with_max in names_with_choc:
result_string = result_string + name_with_max + "\n"
f2 = open('result-2.csv', "w")
f2.write(result_string)
############## # QUESTION 1 # ############## ## https://stackoverflow.com/questions/24662571/python-import-csv-to-list ## but watch out: above link is for python 2.7 import csv with open('candy-data.csv', "r") as f: reader = csv.reader(f) candy_data = list(reader) candy_data.pop(0) candy_data_sparse = list(map(lambda x: [x[0], float(x[12])], candy_data)) ## with list sorting (see lab 9 pt. 3) candy_data_sorted = sorted(candy_data_sparse, reverse=True, key=lambda x: x[1]) name_with_max = candy_data_sorted[0] ## with a for loop name_of_current_max = "none" current_max = 0 for row in candy_data: if float(row[12]) > current_max: current_max = float(row[12]) name_of_current_max = row[0] ############## # QUESTION 2 # ############## f1 = open('result-1.csv', "w") f1.write(name_with_max[0]) ############## # QUESTION 3 # ############## names_with_choc = [] for line in candy_data: if int(line[1]) is 1: names_with_choc.append(line[0]) result_string = "" for name_with_max in names_with_choc: result_string = result_string + name_with_max + "\n" f2 = open('result-2.csv', "w") f2.write(result_string)