python - 轻松拆分 numpy 数组的列

标签 python numpy

如何将数组的列拆分为三个数组 x、y、z 而无需分别手动编写每个 [:,0]、[:,1]、[:,2]

例子

# Create example np array
import numpy as np
data = np.array([[1,2,3],[4,5,6],[7,8,9]])

现在数据

[[1 2 3]
 [4 5 6]
 [7 8 9]]

我想做的事情:

x, y, z = data[:,0], data[:,1], data[:,2] ## Help me here!
print(x)

想要的输出:

array([1, 4, 7])

最佳答案

转置,然后解压:

>>> x, y, z = data.T
>>> x
array([1, 4, 7])

关于python - 轻松拆分 numpy 数组的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30820962/

相关文章:

python - Pandas :在同一张图上绘制两个数据框时出现异常

python - Jupyter 客户端通过 Python 连接到正在运行的内核

python - 将由 float 和文本组成的元组转换为一个字符串

python - 使用 Ruby、Perl 或 Python,如何将 "Move the window ' Firefox' 在屏幕上坐标 (0,0) 并将其调整为 1024 x 768"?

python - 为什么 python 中的 sorted() 不接受位置参数?

python - 使用 numpy 的帕累托边界指数

python - 查找稀疏 csc_matrix 中存在非零条目的行的索引

用于读取或读取行的 Python 自定义分隔符

python - 向张量添加维度并沿新轴复制值

python - 从 Python 中的多维列表中删除前导和尾随零