两个列表之间的减法

时间:2013-10-24 16:18:20

标签: list subtraction

假设我有两个列表:

x = [[1,2,3],[5,4,20],[9,100,7]]
y = [[1,1,1],[1,1,1],[1,1,1]]

我想获得z = x - y以便我的数组应该像

z = [[0, 1, 2], [4, 3, 19], [8, 99, 6]]

1 个答案:

答案 0 :(得分:-1)

例如在Java中

public class Main {

public static void main(String[] args) {

    int[][] x = new int[][]{
                {1,2,3},
                {5,4,20},
                {9,100,7}
            };
    int[][] y = new int[][]{
            {1,1,1},
            {1,1,1},
            {1,1,1}
        };

    int[][] result = new int[3][3];

    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            result[i][j] = x[i][j] - y[i][j];
            System.out.print(result[i][j]+ " ");
        }
        System.out.print(" | ");
    }

}

}