1

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]
5
  • Your description doesn't parse in English; you're missing punctuation and perhaps a few words. Please show the output you expect and the output you get. Describe the process with respect to those values. That should clarify the problem. Commented Oct 30, 2018 at 18:59
  • Why is the output 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? Commented Oct 30, 2018 at 19:03
  • Right but we aren't adding the sums to the new list "pred." The assignment is to add a binary value to the list "pred" if the sum matches the condition. The condition is if the sum of the 3 items before x is greater than k/2, add either 0 or 1 to the list. Commented Oct 30, 2018 at 19:07
  • I'm not adding the sums to pred; I'm trying to follow your instructions. The first three elements sum as 1+0+1 = 2 This is more than k/2, so the result should be a 1. Your updates tell me this should be a 0. Similarly, the second is 0+1+0 = 1, less than k/2, which should be a 0. I don't understand the process. Commented Oct 30, 2018 at 19:09
  • Sorry, user error. Pred should be [1,0]. Commented Oct 30, 2018 at 19:12

2 Answers 2

1

The critical part is taking only the required slice of lyst for your sum. Your start position is the left end for k elements of lyst:

for start in range(0, len(lyst)-k):
    sumList = sum(lyst[start:start+k])

This will move a k-element window through lyst: [1, 0, 1], then [0, 1, 0]. These are greater than k/2 (yielding a 1) and less (for a 0).

Output:

[1, 0]

ADVANCED IMPLEMENTATION

You can build a list comprehension from the inside out, using the code in your (now repaired) loop.

lyst=[1,1,0,0,1]
k=3
pred = [int(sum(lyst[start:start+k]) >= k/2)
            for start in range(0, len(lyst)-k)]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that did it! I knew it was the sum giving me grief.
@gboffi: It took some dialogue to get the idea. I edited to clarify.
0

First you have an error in your code:

for x in range(0, len(lyst)-k) will iterate over the len(lyst)-k first items not the last ones.

You can read about range function to see how it works. Said that you can use slices in order to take the sum of the elements you want.

You code can be changed as:

for x in range(len(lyst) - 1, len(lyst) - k, -1):
    sumList=sum(lyst[x-k:x])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.