Let's say I have a dataset
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
I am trying to write a for loop that will sum the third and fourth numbers in the dataset, then subtract the sum of the first and second numbers in the dataset, then move to the next set of four (so sum the fourth and fifth, then subtract the sum of the second and third from that) down the line until it gets to the last number with three other numbers ahead of it. Problem is, I have no idea how to do either of those things. Here's what I have:
for n in a:
runsum = ((a[3] a[2]) - (a[0] a[1])) ... ((a[15] a[14]) - (a[13] a[12]))
print(runsum)
Obviously, "..." isn't how I let the for loop know it should move down the dataset, but I'm not sure how so I'm using it as a placeholder. Any advice would be much appreciated.
CodePudding user response:
I think this is what you want:
[(w z)-(x y) for x,y,z,w in (a[i:i 4] for i in range(0,len(a),4))]
Which evaluates to [4, 4, 4, 4]
CodePudding user response:
Loop through every 4th position:
for i in range(0,len(a),4):
print((a[i 3] a[i 2])-(a[i] a[i 1]))
CodePudding user response:
The straightforward answer to this question would be to use for
loop and sum all required indices:
s = 0
for i in range(0, len(a) - 3):
s = a[i 2] a[i 3] - a[i] - a[i 1]
Or, as a for comprehension:
sum(a[i 2] a[i 3] - a[i] - a[i 1] for i in range(0, len(a) - 3))
However, if you consider elements that are being added and removed to the sum, you'll notice that most of the elements do not participate in the result, as they are being added and removed the same number of times.
For example, for indices in the range [0..15] here are indices that are added:
[2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15]
And these indices are subtracted:
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13]
You can see that there is overlap in the middle, and some difference at the start and at the end.
You can exploit that and only sum this difference:
s = -sum(a[:3]) - a[1] sum(a[-3:]) a[-2]
This adds last three elements (element before the last twice), and subtracts first three elements (second element twice). This yields the same result as the for loop.
CodePudding user response:
Try this:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
fragments = [a[i:i 4] for i in range(0, len(a), 4)]
result = sum([(f[2] f[1]) - (f[0] f[1]) for f in fragments])
print(result)
# 8
CodePudding user response:
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
sum = 0
for i in range(1,int(len(a)/4) 1 ):
sum = sum ((a[i*4 -1] a[i*4 -2]) - (a[i*4 -4] a[i*4 -3]))
CodePudding user response:
You can use the sum function and striding subscripts stepping by 4:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
r = sum(a[2::4]) sum(a[3::4]) - sum(a[::4]) - sum(a[1::4])
print(r) #16
Each subscript pick up the following indexes:
Indexes: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
Values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
a[2::4) 3 7 11 15 sum=36
a[3::4] 4 8 12 16 sum=40 = 76
a[::4] 1 5 9 13 sum=28
a[1::4] 2 6 10 14 sum=32 - 60
----
16
alternatively you can subtract 2x the negative part from the whole sum
r = sum(a) - 2*( sum(a[::4]) sum(a[1::4]) )
Another way to do it would be to add from a comprehension where you take the negative for the 1st/2nd of each group of 4
r = sum((x,-x)[i%4<2] for i,x in enumerate(a))
CodePudding user response:
With numpy:
import numpy as np
# some sample data
np.random.seed(0)
a = np.random.randint(0, 10, 16)
print(a)
It gives:
[5 0 3 3 7 9 3 5 2 4 7 6 8 8 1 6]
Then compute differences between sums of consecutive numbers:
out = a[2:-1] a[3:] - a[:-3] - a[1:-2]
print(out)
The result:
[ 1 7 10 2 -8 -5 -2 4 7 3 3 -5 -9]
Alternatively, this gives the same result:
out = np.convolve([1, 1, -1, -1], a, 'valid')