'Print multiple columns from a matrix

I have a list of column vectors and I want to print only those column vectors from a matrix. Note: the list can be of random length, and the indices can also be random.

For instance, the following does what I want:

import numpy as np

column_list = [2,3]
a = np.array([[1,2,6,1],[4,5,8,2],[8,3,5,3],[6,5,4,4],[5,2,8,8]])

new_matrix = []
for i in column_list:
    new_matrix.append(a[:,i])
new_matrix = np.array(new_matrix)
new_matrix = new_matrix.transpose()

print(new_matrix)

However, I was wondering if there is a shorter method?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source