【问题标题】:How to convert 1D numpy array of tuples to 2D numpy array?如何将元组的一维 numpy 数组转换为二维 numpy 数组?
【发布时间】:2023-02-03 08:36:14
【问题描述】:

我有一个 numpy 数组 tuples

import numpy as np
the_tuples = np.array([(1, 4), (7, 8)], dtype=[('f0', '<i4'), ('f1', '<i4')])

我想要一个 2D numpy 数组来代替:

the_2Darray = np.array([[1,4],[7,8]])

我试过做几件事,比如

import numpy as np
the_tuples = np.array([(1, 4), (7, 8)], dtype=[('f0', '<i4'), ('f1', '<i4')])
the_2Darray = np.array([*the_tuples])

我怎样才能转换它?

【问题讨论】:

  • np.array([list(item) for item in the_tuples])

标签: python numpy


【解决方案1】:

需要添加the_tuples.astype(object)

import numpy as np
the_tuples = np.array([(1, 4), (7, 8)], dtype=[('f0', '<i4'), ('f1', '<i4')])
the_tuples = the_tuples.astype(object)
the_2Darray = np.array([*the_tuples])

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2018-11-30
    • 2011-12-04
    • 1970-01-01
    • 2019-05-05
    • 2021-05-13
    相关资源
    最近更新 更多