I am trying to have a user defined function that refers to a data set and groups costs by state. The function that I created pulls a list from a list of lists and runs it through the function. While the function works perfectly for the first state, the second time the function is run all of the variables return blank. The function is not adding the proper variables in which is leading to a divide by zero error in my code.
import csv
Patient_File = open('Patient_Data.csv')
reader = csv.reader(Patient_File)
State_Averages = []
States = [['Alabama','ALcost','AL'],['Alaska','akcost','AK']]
def State_Data(State_list,State_Cost,Abbreviation):
State_list = []
State_Cost = []
for row in reader:
if Abbreviation in row[5]:
State_list.append(row)
for item in State_list:
State_Cost.append(item[9])
State_Cost = [elem.strip('$').replace(',','') for elem in State_Cost]
State_Cost=map(float,State_Cost)
#State_Average = sum(State_Cost)/len(State_Cost)
#State_Average = round(State_Average,2)
#State_Averages.append(State_Average)
print Abbreviation
print State_Cost
print State_list
State_Data(States[1][0],States[1][1],States[1][2])
State_Data(States[0][0],States[0][1],States[0][2])
#print State_Averages
The quoted out part of the code is what is causing the divide by zero error but the real core of the problem is the variables State_Cost and State_list are completely empty lists the second time the function is used.