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.
Advertisement
Answer
let me tell you what you’re doing wrong here –
for solo_patient in patients: patients[2] = solo_patient[2].split('/')
here’s in each for loop call i.e 0,1,2. You’re trying to update the last row -> patients[2].
patients[2] = [‘Denise’, ‘Ross’, ’13/02/20′, ‘female’, ‘non-smoker’, ‘150’]
patients[2] value will change if you do something like this inside for loop.
Let’s do this instead –
for solo_patient in patients: print((solo_patient[2]).split('/'))
The above statement will give you another list –
['15', '01', '20'] ['15', '03', '20'] ['13', '02', '20']
Now, if you wanna assign this value back to the original list at a specific index i.e 2 of each row. you can do something like this
for index,solo_patient in enumerate(patients): patients[index][2] = tuple((solo_patient[2]).split('/')) print(patients)
This will print –
[['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']]
To Extract months.
month_list= [] #initialize empty list for solo_patient in patients: date = solo_patient[2].split('/') # split and create list month_list.append(int(date[1])) # select the value at index 1 and convert it to int and then append it to month list. print(month_list)