我创建了一个数据类型,它是:
mytype = np.dtype([('a',np.uint8), ('b',np.uint8), ('c',np.uint8)])
因此,使用这种数据类型的数组:
test1 = np.zeros(3, dtype=mytype)
test1是:
array([(0, 0, 0), (0, 0, 0), (0, 0, 0)],
dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])
现在我有了test2:
test2 = np.array([[1,2,3], [4,5,6], [7,8,9]])
当我使用
test2.astype(mytype)
时,结果并不是我想要的:array([[(1, 1, 1), (2, 2, 2), (3, 3, 3)],
[(4, 4, 4), (5, 5, 5), (6, 6, 6)],
[(7, 7, 7), (8, 8, 8), (9, 9, 9)]],
dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])
I want the result to be:
array([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])
有什么方法吗?谢谢。