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

标签 python numpy

我正在上在线类(class),下面的内容据称演示了“NumPy 数组:仅包含一种类型”:

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

起初,我以为输出是一种错误信息形式,但通过网络搜索并不能证实这一点。事实上,我还没有遇到任何解释......任何人都可以解释如何解释输出吗?

后记:在查看答案后, dtype page , 和 numpy.array() page , 似乎 dtype='<U32'将更准确地描述为 dtype('<U32') .它是否正确?对我来说似乎是这样,但我是新手,甚至 numpy.array() page将字符串分配给 dtype参数而不是实际的 dtype对象。

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

最佳答案

manual 中有完整的解释:

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.

[...]

The first character specifies the kind of data and the remaining characters specify the number of bytes per item, except for Unicode, where it is interpreted as the number of characters. The item size must correspond to an existing type, or an error will be raised. The supported kinds are

[...]

'U'        Unicode string

因此,一个 32 个字符的小端 Unicode 字符串。

关于python - 如何解释 Python 输出 dtype ='<U32'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56944812/

相关文章:

python - functools.wraps 是否有未修饰的引用?

python - 使用 BeautifulSoup 在脚本标签内查找键

python - 使用 NumPy 的 ReLU 导数

pandas - 当输入是 Pandas 数据框时,为什么 Tensorflow 2 会发出警告(但仍然有效)?

python - 双星号

python - seaborn/matplotlib 中的散点图,点大小和颜色由连续数据框列给出

python - 没有 Pandas 的 Seaborn 分组 fiddle 图

Python:计算两个矩阵的相似元素

python - 如何同时使用窗口大小裁剪 numpy 数组的每个元素而不循环遍历每个像素?

python - 是否可以将 class.__init__() 的所有输入都作为一行中的属性?