Please see my attached python script from a larger script I am putting together. This example is simpler than my actual problem that I am trying to solve, but illustrates my lack of understanding of python code:
In the above example, I need to be able to write the value of “test” to the "a"th index of the list “counttest”.
In my more complicated code, I will not know the length of “counttest” and therefore need the length of “counttest” to be populated one by one, possibly being overwritten as it goes.
I am guessing this is where the python code is getting tripped up, any ideas on how to fix? My error is index 0 is out range.
Thanks sal, but maybe my example is too simplified.
In the end, i do not think that I can use the append function as shown, as I have some conditions on if data gets written to a list. I would love if I could get the above example to work with indices and not the append function.
I can post the more complicated example if it is still not clear.
Sounds like you are better off using a for loop instead:
act_values = []
for ind,val in enumerate(value):
if ind == len(value)-1:
act_values.append(val)
continue
elif value[ind+1] > val:
continue
else:
act_values.append(val)
The enumerate function just lets you select the index (ind) and the value (val) at the same time, corresponding to the same iteration of the for loop.
That being said, I wrote this but have no clue what the intention or use case of it is, nor how it reacts to different values. Your explanation didn’t really make sense for me.
Reading it for the 15th time I think I am understanding the logic, in which case my python code above probably doesn’t work for it. Let me see if I can write another way.
Edit: So after trying to think about it and drawing it out, I have come to determine that I still have no damn clue what you are trying to solve with this. The behavior makes no sense from your screenshot of python. However, my python above will do the same behavior as yours but also get you the final number you needed.
I apologize for all the confusion and truly appreciate everyone’s help!
In the end, i got my code to work adding an additional if statement.
my final code is the following: