This is a homework assignment. I am not allowed to use libraries etc. The purpose is to learn and understand nested lists, loops and filtering by conditionals in Python.
I have a nested list of data, a short extract is below. It is 200 lines long.
patients = [ ['Milos', 'Jones', '15/01/20', 'male', 'smoker', '210'],
['Delia', 'Chan', '15/03/20', 'female', 'non-smoker', '170'],
['Denise', 'Ross', '13/02/20', 'female', 'non-smoker', '150'] ]
I need to be able to filter the list by gender and the month. Convert the [5] element in each list to an integer ready.
To filter by gender I have written this code:
female_patients = []
for solo_patient in patients:
if solo_patient[3] == 'female':
female_patients.append(solo_patient)
I have converted the element to an integer using :
for solo_patient in patients:
solo_patient[5] = int(solo_patient[5])
Both work and output what I need.
However, I am trying to split the data into strings and convert to integers so I can filter by month. I used similar logic to above but I can't make it work properly.
for solo_patient in patients:
patients[2] = [solo_patient[2].split('/')
This gives me an error " IndexError: list index out of range"
If I use the code:
for solo_patient in patients:
patients = [solo_patient[2].split('/')
It splits the date into "MM", "DD", "YY" but I lose the other data.
When I do get it split, I need to convert the date strings into integers, then using a loop for range(1,13) go through and group by the month. Then I need to perform basic statistics on the data for male/female. I think I know how to do that once I have filtered the lists correctly. I would appreciate any advice, explanations or constructive feedback.