假设我有一个包含相同长度列表的对象数组:
>>> a = np.empty(2, dtype=object)
>>> a[0] = [1, 2, 3, 4]
>>> a[1] = [5, 6, 7, 8]
>>> a
array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=object)
How can I convert this to a numeric 2D array?
>>> a.shape (2,) >>> b = WHAT_GOES_HERE(a) >>> b array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> b.shape (2, 4)
How can I do the reverse?
Does it get easier if my
a
array is annp.array
ofnp.array
s, rather than annp.array
oflist
s?>>> na = np.empty(2, dtype=object) >>> na[0] = np.array([1, 2, 3, 4]) >>> na[1] = np.array([5, 6, 7, 8]) >>> na array([array([1, 2, 3, 4]), ([5, 6, 7, 8])], dtype=object)
ValueError: 在数组中设置一个序列的元素。
-astype
无法重新塑形。 - Eric