-3

I have a data sheet where I want to do a calculation until I find a zero in one specific column. Then I want to sum all the results of this calculation up to that zero and save the result in an array. I tried with an np array and a list but it does not work:

tst = []
x = data[1:len(data),0]
y = data[1:len(data),1]  
intt = data[1:len(data),2]
for i in range(0,len(data)):
   if intt[i]!=0:         
      tst.append((x[i]**2.0+ y[i]-y[i-1])**2.0)

I want the tst to contain in each position, the sum of the expression in the append().

Thanks!

1
  • 2
    Just add else: break. But also think about what y[i-1] is when i is 0. Commented Jan 17 at 21:26

1 Answer 1

1

I looks like you skip the first datapoint since you write x = data[1:len(data),0] and so forth. As @trincot mentioned, you also have to care about the y[i-1] case for i=0. Maybe the following will help you:

tst = []
x = data[:,0]
y = data[:,1]  
intt = data[:,2]
total = []
for i in range(1,len(data)):
   if intt[i]!=0:         
      tst.append((x[i]**2.0+ y[i]-y[i-1])**2.0)
   else:
      total.append(sum(tst))
      break
total = np.array(total)

This includes all data points in x, y, and intt, but the first data point will still be skipped since the loop starts with i=1.

Edit: A cleaner solution, based on comment from @Barmar.

tst = []
x = data[:,0]
y = data[:,1]  
intt = data[:,2]
for i in range(1,len(data)):
   if intt[i]!=0:         
      tst.append((x[i]**2.0+ y[i]-y[i-1])**2.0)
   else:
      total = np.array(sum(tst))
      break

Edit: Another solution that goes through all of intt:

tst = []
x = data[:,0]
y = data[:,1]  
intt = data[:,2]
total = []
for i in range(1,len(data)):
   if intt[i]!=0:         
      tst.append((x[i]**2.0+ y[i]-y[i-1])**2.0)
   elif i == len(data) - 1:
      total.append(sum(tst))
   else:
      total.append(sum(tst))
      tst = []
      continue
total = np.array(total)
Sign up to request clarification or add additional context in comments.

6 Comments

The problem is that append only appends the result of the calculation. I want to have the sum of all these calculations while the if statement is True.
Do you still ned the tst list then? I have given an update to my answer where I have added a total llist, which simply appends the sum of the tst array when a 0 is encountered.
Why is total a list? You only append to it once.
@Riri asked for it to be an array. Of course, a np.array(total) at the end of the script will turn it into a numpy array. One could just let total be equal to a numpy array of the sum of tst. I have added that possibilty too in my answer now.
@Slavensky I think I don't explain it very well: When I find a zero I want to add up all the numbers I have appended to tst up to that point. And save this sum in a new list. Hope that makes it clearer.
|

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.