且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取结构化数组选择的副本

更新时间:2023-11-07 13:52:22

repack_fields以及多字段视图中的更改:

repack_fields goes along with the change in multifield view:

In [135]: dt = np.dtype([('x', np.float64), ('y', np.float64), ('n', np.uint32)]) 
     ...: arr = np.empty(3, dtype=dt)                                                                  
In [136]: sub = arr[['x','y']]                                                                         
In [137]: import numpy.lib.recfunctions as rf                                                          
In [138]: rf.repack_fields(sub)                                                                        
Out[138]: 
array([(4.04359530e-316, 4.04349886e-316),
       (0.00000000e+000, 0.00000000e+000),
       (4.04355735e-316, 0.00000000e+000)],
      dtype=[('x', '<f8'), ('y', '<f8')])
In [139]: sub                                                                                          
Out[139]: 
array([(4.04359530e-316, 4.04349886e-316),
       (0.00000000e+000, 0.00000000e+000),
       (4.04355735e-316, 0.00000000e+000)],
      dtype={'names':['x','y'], 'formats':['<f8','<f8'], 'offsets':[0,8], 'itemsize':20})

它是副本,而不是视图.

It is a copy, not a view.

对于(n,2)个副本:

And for a (n,2) copy:

In [140]: rf.structured_to_unstructured(sub)                                                           
Out[140]: 
array([[4.04359530e-316, 4.04349886e-316],
       [0.00000000e+000, 0.00000000e+000],
       [4.04355735e-316, 0.00000000e+000]])
In [141]: rf.structured_to_unstructured(rf.repack_fields(sub))                                         
Out[141]: 
array([[4.04359530e-316, 4.04349886e-316],
       [0.00000000e+000, 0.00000000e+000],
       [4.04355735e-316, 0.00000000e+000]])