选择阵列的特定部分

时间:2017-11-16 22:18:57

标签: python arrays

假设我有一个数组

{ "error" : "Content-Type header [application/x-www-formurlencoded] is not supported", "status" : 406

最后我想要一个数组

a = [[[1, 2], [3, 4]],
     [[5, 6], [7, 8]], 
     [[9, 10], [11, 12]],
     [[13, 14], [15, 16]],
     [[17, 18], [19, 20]]]

我以为

b = [[[5, 6]],[[9, 10]]]

会奏效。但是我得到了

b = a[1:3][0:1]

3 个答案:

答案 0 :(得分:0)

所以你想要[[a[1][0]],[a[2][0]]?好吧,首先,我想出了另一种编写数组的方法,使它们更容易解析(视觉上和机械上)。但是,在回答你的问题时,我会使用列表理解。我相信,[a[i][0] for i in [1,2]]会奏效。

答案 1 :(得分:0)

public LoginServlett() {
    super();

}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();


    String name = request.getParameter("UserName");
    String pass = request.getParameter("Password"); 

    RequestDispatcher d=null;

    if (name.contentEquals("Gestionnaire") && pass.contentEquals("1234")) {
         HttpSession session;
         session =request.getSession(true );
          d =request.getRequestDispatcher("/EspaceGestionnaire.html");
         session. setAttribute("NomSauvegardé" ,name);
    }

    else {
         d = request.getRequestDispatcher("/Authentification.html");
        d.forward(request, response);

    } 
}

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

答案 2 :(得分:0)

您还可以看到the answer to this question about transposing lists of lists,并执行以下操作:

at = list(map(list, zip(*a)))
b = at[0][1:3]

虽然正如有人在评论中提到的那样,如果你要进行大量的矩阵操作,你最好不要使用NumPy。