为什么我只看到一个字母而不是"cube" 当我没有输入dtype="str"时,它起作用了. 我的问题是为什么?

代码:

np.full((3,3,3),"cube",dtype=str)

结果:

array([[['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']],

       [['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']],

       [['c', 'c', 'c'],
        ['c', 'c', 'c'],
        ['c', 'c', 'c']]], dtype='<U1')

推荐答案

np.full((3,3,3),"cube",dtype=str)

is equivalent to

np.full((3,3,3),"cube",dtype='U1')

它将只接受字符串的第一个元素.


您可以通过以下方式获得完整的结果:

np.full((3,3,3),"cube",dtype=object)

np.full((3,3,3),"cube",dtype='U4')  # U4 as you have 4 letters in cube, you can do any U>4 like U5, U6, etc.

或 just remove dtype=str. Upon removing it will take the complete length of the string.

np.full((3,3,3),"cube")

Python相关问答推荐

使用unmanagedexports从Python调用的c#DLC

Pandas数据帧处理Pandas表中Json内的嵌套列表以获取后续Numpy数组

获取Azure Pipelines以从pyproject.toml(而不是relevments_dev.文本)安装测试环境

为什么我的(工作)代码(生成交互式情节)在将其放入函数中时不再工作?

使用Python Great Expectations和python-oracledb

Python如何让代码在一个程序中工作而不在其他程序中工作

如何使用stride_tricks.as_strided逆转NumPy数组

在应用循环中间保存pandas DataFrame

使用LineConnection动画1D数据

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

追溯(最近最后一次调用):文件C:\Users\Diplom/PycharmProject\Yolo01\Roboflow-4.py,第4行,在模块导入roboflow中

Pandas - groupby字符串字段并按时间范围 Select

从dict的列中分钟

计算组中唯一值的数量

当独立的网络调用不应该互相阻塞时,'

我对我应该做什么以及我如何做感到困惑'

driver. find_element无法通过class_name找到元素'""

lityter不让我输入左边的方括号,'

ConversationalRetrivalChain引发键错误

在Python中使用yaml渲染(多行字符串)