0

我想向 python numpy 数组添加描述。

例如,当使用 numpy 作为交互式数据语言时,我想做类似的事情:

A = np.array([[1,2,3],[4,5,6]])
A.description = "Holds all the data from experiment 1. Each row contains an intensity measurement with the following columns: time [s], intensity [W/m^2], error [%]."

但它给出了:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.ndarray' object has no attribute 'description'

如果不继承 numpy.ndarray 类,这可能吗?

问候,乔纳斯

4

1 回答 1

3

最简单的方法是使用 anamedtuple来保存数组和描述符:

>>> from collections import namedtuple
>>> Array = namedtuple('Array', ['data', 'description'])
>>> A = Array(np.array([[1,2,3],[4,5,6]]), "Holds all the data from experiment 1. Each row contains an intensity measurement with the following columns: time [s], intensity [W/m^2], error [%].")
>>> A.data
array([[1, 2, 3],
       [4, 5, 6]])
>>> A.description
'Holds all the data from experiment 1. Each row contains an intensity measurement with the following columns: time [s], intensity [W/m^2], error [%].'
于 2013-10-05T12:27:46.603 回答