Arrays 如何从数组中删除列

Arrays 如何从数组中删除列,arrays,python-3.x,numpy,Arrays,Python 3.x,Numpy,这是(3x9)阵列: 我想要这样的(3x3): 我写了一些代码。 有更好的方法吗 AB = x[:,2:] #Removes the first 2 columns print(AB) C = np.delete(AB, 1, 1) print(C) D = np.delete(C, 1, 1) print(D) E = np.delete(D, 1, 1) print(E) F = np.delete(E, 1, 1)

这是(3x9)阵列:

我想要这样的(3x3):

我写了一些代码。 有更好的方法吗

    AB = x[:,2:] #Removes the first 2 columns
    print(AB)
    C = np.delete(AB, 1, 1)
    print(C)
    D = np.delete(C, 1, 1)
    print(D)
    E = np.delete(D, 1, 1)
    print(E)
    F = np.delete(E, 1, 1)
    print(F)

您可以在普通python中使用
zip
enumerate
执行
bulk
删除操作:

cols_to_del = [0, 1, 3, 4, 5, 6]
AB_trans = [v for i, v in enumerate(zip(*AB)) if i not in cols_to_del]
AB = np.array(list(zip(*AB_trans)))
print(AB)
# array([[ 2,  7,  8],
#        [11, 16, 17],
#        [20, 25, 26]])

其思想是转置数组,并删除列(现在显示为行)。

您可以在普通python中使用
zip
enumerate
执行
bulk
删除:

cols_to_del = [0, 1, 3, 4, 5, 6]
AB_trans = [v for i, v in enumerate(zip(*AB)) if i not in cols_to_del]
AB = np.array(list(zip(*AB_trans)))
print(AB)
# array([[ 2,  7,  8],
#        [11, 16, 17],
#        [20, 25, 26]])

其想法是转置数组,并删除列(现在显示为行)。

谢谢。我用了两行代码。尽管你很聪明,谢谢你。我用了两行代码。虽然你的“聪明”是的,但看起来更干净(+1)。修复您的凹痕是的,看起来更干净(+1)。修正你的缩进
cols_to_del = [0, 1, 3, 4, 5, 6]
AB_trans = [v for i, v in enumerate(zip(*AB)) if i not in cols_to_del]
AB = np.array(list(zip(*AB_trans)))
print(AB)
# array([[ 2,  7,  8],
#        [11, 16, 17],
#        [20, 25, 26]])
    index = [0, 1, 3, 4, 5, 6]     #Set the index of columns I want to remove
    new_a = np.delete(x, index, 1) #x=name of array
                                   #index=calls the index array
                                   #1=is the axis. So the columns
    print(new_a)                   #Print desired array