Home > OS >  trying to perform a calculation using values from a list with conditional output (also a list) value
trying to perform a calculation using values from a list with conditional output (also a list) value

Time:08-15

I am trying to get an output list by using conditions to determine the values. For some conditions I want to set the output to a constant and for other conditions I want to perform a calculation from values in another list of values with the same index. When it gets to the conditions that set the output to an integer I get "TypeError: 'int' object does not support item assignment". Here is the code. How do I fix this?

#Load DL values
dl = DL.values()

#Get the length of the dataset
dataset_length2 = DL.size()

#Create temporary lists to store the results
tf_calc = [-9999] * dataset_length2

for idx, val in enumerate(dl):
    if dl[idx] == -9999:
        tf_calc[idx] = -9999
    else:
        if delta_azi[idx] == 0 and st_inc[idx] - st_inc[idx-1] > 0:
            tf_calc[idx] = 0 #<-------------------------------------I get the error here
        elif delta_azi[idx] > 0 and st_inc[idx] - st_inc[idx-1] == 0:
            tf_calc[idx] = 90
        elif delta_azi[idx] == 0 and st_inc[idx] - st_inc[idx-1] < 0:
            tf_calc[idx] = 180
        elif delta_azi[idx] > 0 and st_inc[idx] - st_inc[idx-1] == 0:
            tf_calc[idx] = 270
        elif st_azi[idx] - st_azi[idx-1] > 0 and st_inc[idx] - st_inc[idx-1] > 0:
            tf_calc[idx] = acos((cos(radians(st_inc[idx-1]))*cos(radians(dl[idx-1]))-cos(radians(st_inc[idx])))/(sin(radians(st_inc[idx-1]))*sin(radians(dl[idx-1]))))
        else:  #temporary - more conditions to come
            tf_calc = -9999
        
TF_CALC.setValues(tf_calc)
TF_CALC.save()

CodePudding user response:

The last 'else' condition you wrote as "temporary -more condition to come" is replacing your list saved in tf_calc with the number -9999.

If you replace these lines with:

        else:  #temporary - more conditions to come
            tf_calc[idx] = -9999

you should solve your problem.

  • Related