为什么此python代码仅产生最后的结果

问题描述 投票:0回答:1

我想找到满足条件result_x = 1-initial_x的矩阵; result_y = initial_y; results_z = initial_y。但是,我的代码仅得出每个数组的最后一个。您能帮我吗?

import numpy as np
import math

def reverse_a_direction(matrix):
    reverse_a = []
    for (x, y, z) in matrix:
        reverse_a = 1 - x, y, z
    return reverse_a
a = np.array([[(0.1666666666666667, 0.8012144614989793, 0.7500000000000000), 
(0.1666666666666667, 0.1987855385010207, 0.2500000000000000)], 
[(0.6666666666666666, 0.3012144614989793, 0.7500000000000000), 
(0.6666666666666666, 0.6987855385010207, 0.2500000000000000)]])

for i in range(0, len(a)):
    print(reverse_a_direction(a[i]))

此代码的结果:

(0.8333333333333333, 0.1987855385010207, 0.25)
(0.3333333333333333, 0.6987855385010207, 0.25)

预期结果:

[(0.8333333333333333, 0.8012144614989793, 0.75), (0.8333333333333333, 0.1987855385010207, 0.25)],
[(0.3333333333333333, 0.3012144614989793, 0.75), (0.3333333333333333, 0.6987855385010207, 0.25)]
python
1个回答
0
投票

您将在每次迭代中覆盖reverse_a。正确的解决方案是:

def reverse_a_direction(matrix):
    reverse_a = []
    for (x, y, z) in matrix:
        a = 1 - x, y, z
        reverse_a.append(a)
    return reverse_a
© www.soinside.com 2019 - 2024. All rights reserved.