今天看numpy文档时,遇到一个问题 https://numpy.org/doc/stable/reference/generated/numpy.argsort.html#numpy.argsort,在初始化array时的参数dtype=[('x','i4'),('y','i4')],做个记录:
x = np.array([(1, 1000), (0, 1111)], dtype=[('x', '<i4'), ('y', '<i4')])
这里的dtype的作用:给array中初始化值指定名字和数据类型。每一项初始化值的第一项被指定为'x',第二项被指定为'y'。
分别使用名字索引访问x中的元素:
print(x['x']) # 输出 [1 0]
print(x['y']) # 输出 [1000 1111]
还可以通过'x'修改x中的值
x['x'] = np.array([2000, 2222])
print(x) # 输出 [(2000, 1000) (2222, 1111)]
'<i4'表示具体的数据类型'little-endian 32-bit signed integer',具体见 https://numpy.org/doc/stable/reference/arrays.dtypes.html#arrays-dtypes-constructing:
参考:
https://stackoverflow.com/questions/51364975/datatype-in-numpy