1 np.dtype({'names':('name', 'age', 'weight'),'formats':('U10', 'i4', 'f8')}) 2 #Output: 3 dtype([('name', '<U10'), ('age', '<i4'), ('weight', '<f8')]) 4 5 np.dtype([('name', 'S10'), ('age', 'i4'), ('weight', 'f8')]) 6 #Output: 7 dtype([('name', 'S10'), ('age', '<i4'), ('weight', '<f8')])
NumPy 还提供np.recarray 类。
1 #input: 2 name = ['Alice', 'Bob', 'Cathy', 'Doug'] 3 age = [25, 45, 37, 19] 4 weight = [55.0, 85.5, 68.0, 61.5] 5 data['name'] = name 6 data['age'] = age 7 data['weight'] = weight 8 data_rec = data.view(np.recarray) #np.recarry类 9 data_rec.age 10 11 #output: 12 array[25, 45, 37, 19]