For loop arrays operation

Question:

I would like to write these expressions in a nicer way, with a for loop.
I have to do it 4 times for u1, u2, v1 and v2 and store these 3 arrays each time.

delta_u1_up_down = [u1[2] - u1[1], u1[4] - u1[3], u1[6] - u1[5],
                        u1[8] - u1[7], u1[10] - u1[9], u1[12] - u1[11], u1[14] - u1[13],
                        u1[16] - u1[15], u1[18] - u1[17]]

delta_u1_first = [u1[1] - u1[17], u1[3] - u1[17], u1[5] - u1[17], u1[7] - u1[17],
                      u1[9] - u1[17], u1[11] - u1[17], u1[13] - u1[17], u1[15] - u1[17],
                      u1[2] - u1[18], u1[4] - u1[18], u1[6] - u1[18], u1[8] - u1[18],
                      u1[10] - u1[18], u1[12] - u1[18], u1[14] - u1[18],
                      u1[16] - u1[18]]

delta_u1_previous = [u1[1] - u1[3], u1[3] - u1[5], u1[5] - u1[7], u1[7] - u1[9],
                         u1[9] - u1[11], u1[11] - u1[13], u1[13] - u1[15],
                         u1[15] - u1[17], u1[2] - u1[4], u1[4] - u1[6], u1[6] - u1[8],
                         u1[8] - u1[10], u1[10] - u1[12], u1[12] - u1[14],
                         u1[14] - u1[16], u1[16] - u1[18]]

Can someone please help me? The order is always the same.

Asked By: TFAE

||

Answers:

Check out range(start, stop, step):

delta_u1_up_down  = [u1[i] - u1[i-1] for i in range(2, 19, 2)]
delta_u1_first    = [u1[i] - u1[17] for i in range(1, 17, 2)] +
                    [u1[i] - u1[18] for i in range(2, 18, 2)]
delta_u1_previous = [u1[i] - u1[i+2] for i in range(1, 17, 2)] +
                    [u1[i] - u1[i+2] for i in range(2, 18, 2)]
Answered By: user2390182

Use slices and zip

You can do that kind of manipulation with slicing and zip

delta_u1_up_down = [e2 - e1 for e1, e2 in zip(u1[1::2], u1[2::2])]
delta_u1_first = [e - u1[-3] for e in u1[1:-3:2]] + [e - u1[-2] for e in u1[2:-2:2]]
delta_u1_previous = [e1 - e2 for e1, e2 in zip(u1[1::2], u1[3::2])] + [e1 - e2 for e1, e2 in zip(u1[2::2], u1[4::2])]

Here a test to check the result, assuming the length of u is 20.

u1 = list(range(20))

delta_u1_up_down = [u1[2] - u1[1], u1[4] - u1[3], u1[6] - u1[5],
                        u1[8] - u1[7], u1[10] - u1[9], u1[12] - u1[11], u1[14] - u1[13],
                        u1[16] - u1[15], u1[18] - u1[17]]

delta_u1_first = [u1[1] - u1[17], u1[3] - u1[17], u1[5] - u1[17], u1[7] - u1[17],
                      u1[9] - u1[17], u1[11] - u1[17], u1[13] - u1[17], u1[15] - u1[17],
                      u1[2] - u1[18], u1[4] - u1[18], u1[6] - u1[18], u1[8] - u1[18],
                      u1[10] - u1[18], u1[12] - u1[18], u1[14] - u1[18],
                      u1[16] - u1[18]]

delta_u1_previous = [u1[1] - u1[3], u1[3] - u1[5], u1[5] - u1[7], u1[7] - u1[9],
                         u1[9] - u1[11], u1[11] - u1[13], u1[13] - u1[15],
                         u1[15] - u1[17], u1[2] - u1[4], u1[4] - u1[6], u1[6] - u1[8],
                         u1[8] - u1[10], u1[10] - u1[12], u1[12] - u1[14],
                         u1[14] - u1[16], u1[16] - u1[18]]

print(delta_u1_up_down)  # [1, 1, 1, 1, 1, 1, 1, 1, 1]
delta_u1_up_down = [e2 - e1 for e1, e2 in zip(u1[1:-1:2], u1[2:-1:2])]
print(delta_u1_up_down)  # [1, 1, 1, 1, 1, 1, 1, 1, 1]

print(delta_u1_first)  # [-16, -14, -12, -10, -8, -6, -4, -2, -16, -14, -12, -10, -8, -6, -4, -2]
delta_u1_first = [e - u1[-3] for e in u1[1:-3:2]] + [e - u1[-2] for e in u1[2:-2:2]]
print(delta_u1_first)  # [-16, -14, -12, -10, -8, -6, -4, -2, -16, -14, -12, -10, -8, -6, -4, -2]

print(delta_u1_previous)  # [-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2]
delta_u1_previous = [e1 - e2 for e1, e2 in zip(u1[1:-1:2], u1[3:-1:2])] + [e1 - e2 for e1, e2 in zip(u1[2:-1:2], u1[4:-1:2])]
print(delta_u1_previous)  # [-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2]
Answered By: Dorian Turba
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.