python For循环数组操作

j91ykkif  于 2023-02-11  发布在  Python
关注(0)|答案(2)|浏览(131)

我想用更好的方式写这些表达式,用一个for循环,我必须为u1,u2,v1和v2做4次,每次都存储这3个数组。

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]]

有谁能帮帮我吗?顺序总是一样的。

92dk7w1h

92dk7w1h1#

查看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)]
az31mfrm

az31mfrm2#

使用切片和压缩

你可以用切片和压缩来完成这种操作

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])]

下面是一个检验结果的测试,假设u的长度为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]

相关问题