【问题标题】:Numpy 1d array to 2d array with first element being isolatedNumpy 1d 数组到 2d 数组,第一个元素被隔离
【发布时间】:2022-01-24 01:21:29
【问题描述】:

我从类似的东西开始

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

我想要的是这样的:

x = np.array([5, [[1, 2], [3, 4]], [6, [[2, 4],[6, 8]] ]

我现在有事

y = np.array([x[0][0], x[0][1:]], [x[1][0], x[1][1:]])

【问题讨论】:

  • 我只想把它分成两个数组。 Numpy 不适合处理锯齿状的形状
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python numpy


【解决方案1】:

你错过了[]

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

列表理解

[[row[0],row[1:].reshape(2,2).tolist()] for row in x]

【讨论】:

    猜你喜欢
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 2017-06-18
    • 2021-10-12
    相关资源
    最近更新 更多