How to populate an existing numpy array with specific dtype

后端 未结 2 1171
有刺的猬
有刺的猬 2021-01-25 02:23

Let\'s say that I have this initial numpy array with some fixed dtype:

array = numpy.array([(1, \'a\'), (2, \'b\')],
                    numpy.dtype([(\'idfield\         


        
2条回答
  •  梦谈多话
    2021-01-25 02:51

    Like @juanpa.arrivillaga commented, it's cleaner to define your dtype only once:

    array_dt = np.dtype([
        ('idfield', np.int32),
        ('textfield', '|S256')
    ])
    

    Then define your second list of values as an array and then concatenate

    array2 = np.array(value, array_dt)                                     
    array = np.concatenate([array, array2])
    

提交回复
热议问题