CSE-8A / PA3 / Testing 2.0 PA3.py
Testing 2.0 PA3.py
Raw
#list1 = ["Males", "Females", "Males", "Males", "Males", "Females", "Females"]
#list2 = list1
#num_males = len(list1) # shows the number of stuff in list1


#num_males = list1.count("Males")
# BEGIN PROVIDED CODE
# ingest_data: given a file name and a field name, returns a list containing
# all the data in the given file for the given field name
import csv
def ingest_data(filename, fieldname):
    file_object = open(filename, newline='')
    rows = csv.reader(file_object, delimiter=',')
    headers = next(rows)
    try:
        field_idx = headers.index(fieldname)
    except ValueError:
        print('The field name', fieldname, 'does not exist in the headers.')
        print('Here are the value field names in this file:')
        for h in headers:
            print(h)
        return
    data_list = []
    count = 0
    limit = 5106 # CHANGE LIMIT
    for line in rows:
        if (count >= limit):
            print('Too many entries, returning first', limit, 'entries.')
            return data_list
        try:
            field_value = line[field_idx]
        except IndexError:
            print('Skipping row #', count, 'because field does not exist')
            continue
        data_list.append(field_value)
        count = count + 1
    return data_list
#Ingest data code ends here
Gender = ingest_data("data.csv", "Sex")
Location = ingest_data("data.csv", "Location")

list1 = Gender
list2 = Location

#the locations and then number of people that visited it
num_hospital = Location.count("Hospital")
num_residence = Location.count("Residence")
num_other = Location.count("Other")
num_location_unknown = 5106- (num_hospital + num_residence + num_other)

num_males = Gender.count('Male')
num_females = Gender.count('Female')
num_gender_unknown = 5106-(num_males + num_females)
i= 0
j= 0

Total_num_males_hospital = 0
Total_num_males_residence = 0
Total_num_males_other = 0
Total_num_males_unknown = 0
Total_num_females_hospital = 0
Total_num_females_residence = 0
Total_num_females_other = 0
Total_num_females_unknown = 0
Total_num_unknown= 0

#Male = Gender.index('Male')
#Hospital = Location.index('Hospital')

#def analyze_data(Gender, Location):
for i in Gender and Location:
    if Gender[i] == ('Male') and Location[i] == ('Hospital'):
        Total_num_males_hospital += 1
        #return(Total_num_males_hospital)

    if Gender[i] ==('Male') and Location[i] == ('Residence'):
        Total_num_males_residence += 1
          
    if Gender[i] == ('Male') and Location[i] == ('Other'):
        Total_num_males_other += 1
         
    if Gender[i] == ('Male') and Location[i] == (''):
        Total_num_males_unknown += 1
           
    if Gender[i] == ('Female') and Location[i] == ('Hospital'):
        Total_num_females_hospital += 1
        
    if Gender[i] ==('Female') and Location[i] == ('Residence'):
        Total_num_females_residence += 1
          
    if Gender[i] == ('Female') and Location[i] == ('Other'):
        Total_num_females_other += 1
         
    if Gender[i] == ('Female') and Location[i] == (''):
        Total_num_females_unknown += 1
        
    else:
        if Gender[i] == ('') and Location[i] == (''):
            Total_num_unknown += 1


#def analyze_data(Gender, Location):
  #  for a in Gender and b in Location:
   #     if Gender[a] == ('Male') and Location[b] == ('Hospital'): 
    #        return(Gender[Male:Male], Location[Hospital:Hospital])

print(Total_num_females_unknown + Total_num_males_unknown, "is the number of people that went to unidentified places")
print(num_gender_unknown, "is the number of people that could not be identified based on gender")
       # i = Gender[i -1]