python - 如何在 numpy python3+ 中为数组列表创建 dtype

标签 python python-3.x numpy numpy-ndarray numpy-dtype

我有一个从串口获取并共享的 300x57 字节数组,然后我打算将其直接解封装到 numpy 数组中

所以我创建了一个 57 字节大小的数据类型

onebuffdtype = np.dtype(
        "b1, b1, f4, u4, u2, u1, f4, u4, u2, u1, f4, i4, i2, i1, f4, u4, u2, u1, f4, u4, u2, u1")

那么如果

shared_buff1.buf

是缓冲区所在的地方

buff1_np = np.ndarray((300, 57,), dtype=onebuffdtype,
                          buffer=shared_buff1.buf)

但它说

TypeError: buffer is too small for requested array

最佳答案

数组的形状是元素的数量,而不是它包含的字节数。如果您的元素大小均为 57 字节,则 300x57 字节缓冲区正好适合初始化大小为 300 的数组:

buff1_np = np.ndarray(300, dtype=onebuffdtype, buffer=shared_buff1.buf)

关于python - 如何在 numpy python3+ 中为数组列表创建 dtype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58772649/

相关文章:

python - OpenCV,更改为图像中最接近的颜色

python - 使用 Scapy 构建 Probe Request 请求

python - ValueError : Target is multiclass but average ='binary' . 请选择另一个平均值设置

python - 将文件从 Google Compute Engine VM 上运行的 docker 镜像复制到 Google Storage 存储桶

python-3.x - 如何将SEC txt文件读入 Pandas 数据框?

python - 使用 Cython 包装 C++ 模板以接受任何 numpy 数组

python - 比较多维列表范围的最快方法

python - 通过删除循环来优化 Python 代码

python - 将元组组合成列表

python - 如何从一系列点计算从一个点到每个其他点的相对向量