【发布时间】:2014-05-15 21:31:42
【问题描述】:
如果有任何帮助,我将不胜感激 :)
我正在尝试从一维字符串数组创建一个记录数组 和二维数字数组(所以我可以使用 np.savetxt 并将其转储到文件中)。 不幸的是,文档没有提供信息:np.core.records.fromarrays
>>> import numpy as np
>>> x = ['a', 'b', 'c']
>>> y = np.arange(9).reshape((3,3))
>>> print x
['a', 'b', 'c']
>>> print y
[[0 1 2]
[3 4 5]
[6 7 8]]
>>> records = np.core.records.fromarrays([x,y])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/numpy/core/records.py", line 560, in fromarrays
raise ValueError, "array-shape mismatch in array %d" % k
ValueError: array-shape mismatch in array 1
我需要的输出是:
[['a', 0, 1, 2]
['b', 3, 4, 5]
['c', 6, 7, 8]]
【问题讨论】:
-
x应该是一个数组,对吧?目前它是一个列表。 -
正确,由于某种原因我无法编辑我的帖子...
-
@unutbu 的回答很有帮助!它让我寻找一种更优雅的解决方案来将二维数组与其列分开,我发现了这个:records = np.core.records.fromarrays([x]+[row for row in y.transpose()])
标签: python arrays numpy record