import csv def read_csv(filename, skip = 1): ''' given the name of a csv file, return its contents as a 2d list, including the header.''' data = [] with open(filename, "r") as infile: csvfile = csv.reader(infile) for _ in range(skip): next(csvfile) for row in csvfile: data.append(row) return data def lst_to_dct(lst): ''' given a 2d list, create and return a dictionary. keys of the dictionary come from the header (first row) , values are corresponding columns, saved as lists Ex: [[1, 2, 3], [x, y, z], [a, b, c]] should return {1 : [x, a], 2 : [y, b], 3 : [z, c]} ''' dct = {h : [] for h in lst[0]} for row in lst[1:]: for i in range(len(row)): dct[lst[0][i]].append(row[i]) return dct def col_to_lst(lst, col_no): ''' given a 2d list and a column number, return a list consisting of the values in the specified column Ex: [[1, 2, 3], [4, 5, 6]], 1 then return [2, 5] ''' newlst = [] for row in lst: newlst.append(row[col_no]) return newlst def clean_currency(s): ''' given a string in $xx,xxx compute its float value and return it ''' s = s.replace("$", "") s = s.replace(",", "") return float(s) def normalize(lst): ''' given a list of numeric values, create a list of normalized values using min/max normalization and return it x_i = (x_i - x_min) / (x_max - x_min) ''' normal = [] mx = max(lst) mn = min(lst) for num in lst: normalized = (num - mn) / (mx - mn) normal.append(normalized) return normal def main(): print("Hello I am the utility function file!") if __name__ == "__main__": main()