在Python中,你可以使用列表推导式或者NumPy库来取出二维数组的某一列。以下是两种方法的示例:
使用列表推导式
def get_column(matrix, col_index):
return [row[col_index] for row in matrix]
示例使用
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
column = get_column(matrix, 1)
print(column) 输出: [2, 5, 8]
使用NumPy库
import numpy as np
示例使用
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
column = a[:, 1] 取出第二列
print(column) 输出: [2 5 8]
请注意,在使用NumPy时,索引是从0开始的,所以`a[:, 1]`表示取出第二列。