기존의 배열을 여러 개의 명명 된 필드로 가져 와서 원래 dtype과 동일한 계층 적 dtype을 가진 하나의 필드로 새 배열을 만들거나 변경하십시오. 즉 oldarray
여기numpy 구조화 된 배열에 계층 구조 추가
발 모양과 구조가 동일하다
newarray = np.array(oldarray, dtype=[('old',oldarray.dtype)])
같은 newarray['old']
것을하는 예이다 :
In [1]: import numpy as np
In [2]: dt = np.dtype([('name',np.str_,2),('val',np.float_)])
In [3]: constants = np.array([('pi',3.14),('e',2.72)],dtype=dt)
In [4]: constants
Out[4]:
array([('pi', 3.14), ('e', 2.72)],
dtype=[('name', '|S2'), ('val', '<f8')])
In [5]: numbers = constants.astype([('constants',dt)])
하지만 날 모두 제로 준다 :
In [6]: numbers
Out[6]:
array([(('', 0.0),), (('', 0.0),)],
dtype=[('constants', [('name', '|S2'), ('val', '<f8')])])
을 복사본을 만들려고해도 같은 문제가 발생합니다.
In [7]: numbers = np.array(constants,dtype=[('constants',dt)])
In [8]: numbers
Out[8]:
array([(('', 0.0),), (('', 0.0),)],
dtype=[('constants', [('name', '|S2'), ('val', '<f8')])])
또한 : 사람이 이런 일이 이유 알고 있나요?
내가 원래 배열의 목록을 만들어 문제를 해결할 수
감사합니다. @joris, 이것이 완벽합니다. 뷰를 사용하면 실제로 배열을 내부적으로 변경할 수 있습니다 :'constants = constants.view ([('constants', dt)])' – askewchan