I want to focus on a moving window of k in the lyst below, starting on the left and stopping one element short of the right edge. I want to take the sum of the 3 (k) items in the window. If the sum is >= k/2, append the value of 1 to the list "pred", otherwise append 0 to the list.
Here is my code so far:
lyst=[1,0,1,0,1]
k=3
pred=[]
for x in range(0, len(lyst)-k):
sumList=sum(lyst)
if sumList >= k/2:
pred.append(1)
else:
pred.append(0)
print(pred)
I know my sumList item is the issue here. I just need to adjust that so it generally sums k items to create 2 (that's len(lyst)-k) new values to pred. Each value will either be 0 or 1 depending on the condition.
Output should be:
pred=[1, 0]
Output I'm getting now:
pred=[1,1]
0, 1? It looks to me like it shoudl be the opposite. If we add the first 3 positions, the sum is more than k/2; the sum of positions 1-3 is less. Therefore, the output would be[1, 0]. What is my misunderstanding?pred; I'm trying to follow your instructions. The first three elements sum as1+0+1 = 2This is more thank/2, so the result should be a1. Your updates tell me this should be a0. Similarly, the second is0+1+0 = 1, less than k/2, which should be a0. I don't understand the process.