jeudi 1 mars 2018

Making a list based on a criteria of another list

I have an empty list called 'label', where depending on which bucket is more appropriate, it will fill the 'label' list with 0,1,or 2.

'label' is associated to the 3 'buckets':

# Bucket 0: 0 -7 Days --------------------------> 0
# Bucket 1: 1 - 6 Weeks (8 - 42 Days) ----------> 1
# Bucket 2: 7+ Weeks (49+ Days) ----------------> 2

I have another list has a length of 23411. This list's contents consist of 0 days to 1099 days. So based on this list's contents, it should populate the 'label' list.

I have tried this for-loop & if-else statement to do what I want, however it is giving me a IndexError: list assignment index out of range:

label = []
for i in range(23411):
    if ageNew[i] <= 7:
        label[i] = 0
    elif ageNew[i] <= 42:
        label[i] = 1
    else:
        label[i] = 2

For example:

list: [0, 8, 14, 14, 45, 1056, 1]
label: [0, 1, 1, 1, 2, 2, 0]

Aucun commentaire:

Enregistrer un commentaire