对array取其中的某一列

import numpy as np
table=[[0,1,2,3],[4,5,6,7],[8,9,10,11]]
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]
table1 = np.array(table)
# print(table1)
# print(table1[:,0]) #取 table 的第一列[0 4 8]
# print(table1[0,:]) #取 table 的第一行[0 1 2 3]

a=table1[..., 0:2]#取 table 的第1,2列
# [[0 1]
#  [4 5]
# [8 9]]
b=table1[..., 2:4]#取 table 的第2,3列
# [[ 2  3]
#  [ 6  7]
#  [10 11]]
c=a+b
print(c) #将a和b的对应元素相加
# [[ 2  4]
#  [10 12]
#  [18 20]]

猜你喜欢

转载自blog.csdn.net/weixin_38145317/article/details/88695020