How do you extract a column from a multi-dimensional array?

前端 未结 20 2876
感情败类
感情败类 2020-12-02 03:56

Does anybody know how to extract a column from a multi-dimensional array in Python?

20条回答
  •  天命终不由人
    2020-12-02 04:18

    array = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
    
    col1 = [val[1] for val in array]
    col2 = [val[2] for val in array]
    col3 = [val[3] for val in array]
    col4 = [val[4] for val in array]
    print(col1)
    print(col2)
    print(col3)
    print(col4)
    
    Output:
    [1, 5, 9, 13]
    [2, 6, 10, 14]
    [3, 7, 11, 15]
    [4, 8, 12, 16]
    

提交回复
热议问题