如何解释 Python 输出 dtype='<U32'?

use*_*800 6 python numpy

我正在参加一门在线课程,以下内容据称表明“NumPy 数组:仅包含一种类型”:

In [19]: np.array([1.0, "is", True])
Out[19]:
array(['1.0', 'is', 'True'],
dtype='<U32')
Run Code Online (Sandbox Code Playgroud)

起初,我以为输出是一种错误信息,但这并没有得到网络搜索的证实。事实上,我还没有遇到过解释……谁能解释一下如何解释输出?

后记:在查看答案、dtype页面numpy.array()页面后,似乎dtype='<U32'更准确地描述为dtype('<U32')。这样对吗?在我看来是这样,但我是一个新手,甚至numpy.array()页面dtype参数分配了一个字符串,而不是一个实际的dtype对象。

另外,'<U32'当所有元素都是短得多的字符串时,为什么要指定 32 个字符的字符串?

adl*_*z15 9

dtype='<U32'是一个小尾数 32 个字符的字符串。

有关数据类型的文档更深入地介绍了每个字符。

'U' Unicode string

Several kinds of strings can be converted. Recognized strings can be prepended with '>' (big-endian), '<' (little-endian), or '=' (hardware-native, the default), to specify the byte order.

例子:

dt = np.dtype('f8')   # 64-bit floating-point number
dt = np.dtype('c16')  # 128-bit complex floating-point number
dt = np.dtype('a25')  # 25-length zero-terminated bytes
dt = np.dtype('U25')  # 25-character string```
Run Code Online (Sandbox Code Playgroud)


Ama*_*dan 8

手册中对此进行了充分说明:

可以转换多种字符串。可识别的字符串可以在前面加上'>'(big-endian)、'<'(little-endian) 或'='(hardware-native,默认),以指定字节顺序。

[...]

第一个字符指定数据的类型,其余字符指定每个项目的字节数,Unicode 除外,它被解释为字符数。项目大小必须与现有类型相对应,否则将引发错误。支持的种类有

[...]

'U'        Unicode string
Run Code Online (Sandbox Code Playgroud)

所以,一个 32 个字符的小端 Unicode 字符串。