在numpy 2D数组中按索引比较两个元素

时间:2019-07-29 07:20:14

标签: numpy

我目前正在学习这门课程以学习numpy:https://saskeli.github.io/data-analysis-with-python-summer-2019/numpy2.html。我目前处于练习1(列比较)中。

练习状态:编写函数column_comparison,该函数获取一个二维数组作为参数。该函数应返回一个新数组,其中包含来自输​​入的行,这些行的第二列的值大于最后第二列的值。

这是测试数组:

arr = np.array([[8 ,9 ,3 ,9 ,8],
                [0 ,5 ,3 ,9 ,9],
                [5 ,7 ,6 ,0 ,4],
                [7 ,8 ,1 ,6 ,2],
                [2 ,1 ,3 ,5 ,8],
                              ])

我尝试了这些,它们显示了相同的输出:

c = arr[1] > arr[-2]      arr[arr[1] > arr[-2]]

[[5 7 6 0 4]
 [7 8 1 6 2]
 [2 1 3 5 8]]

结果应为:

[[8 9 3 8 8]
[5 7 6 0 4]
[7 8 1 6 2]]

我什至不知道该怎么做,最后一行当然不满足我想要的条件。我觉得我缺少一些显而易见的东西,但是我无法提出一个可行的解决方案。

1 个答案:

答案 0 :(得分:1)

您没有选择列

int grnNo;
Connection con = null;
Statement statement = null;

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String[] itemCode = request.getParameterValues("itemCodetd");
    String[] unitCode = request.getParameterValues("unittd");

    try {
        con = DBConnection.createConnection();
        statement = con.createStatement();

        String grnNoSql = "select max(GRNNUMBER)+1 as GRNNo from egoodsreceived";
        ResultSet resultSet1 = statement.executeQuery(grnNoSql);

        while (resultSet1.next()) {
            int grnNoLocal = resultSet1.getInt("GRNNo");

            if (grnNo != 0) {
                grnNo = grnNoLocal;
                System.out.println("in if  :" + grnNo);
            } else {
                grnNo = 1;
                System.out.println("in else  :" + grnNo);
            }

        }

        String query1 = "query to insert data";

        PreparedStatement ps = con.prepareStatement(query1);

        for (int i = 0; i < itemCode.length; i++) {
            if (itemCode[i] != "") {
                System.out.println("in for :" + grnNo);
                ps.setInt(1, grnNo);
                ps.setString(2, itemCode[i]);
                ps.setString(3, unitCode[i]);

                ps.addBatch();
            }
        }

        ps.executeBatch(); // here getting erro as duplicate primary key

    } catch (SQLException e) {
        System.out.println("SQL EXCPTION   91");
        e.printStackTrace();
    }

    doGet(request, response);
}