ctypes-numpy数组没有形状? [英] ctypes - numpy array with no shape?

查看:75
本文介绍了ctypes-numpy数组没有形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python包装器来调用c ++ dll库的函数. dll库返回一个ctype,我将其转换为numpy数组

I am using a python wrapper to call functions of a c++ dll library. A ctype is returned by the dll library, which I convert to numpy array

score = np.ctypeslib.as_array(score,1) 

但是,数组没有形状吗?

however, the array has no shape?

score
>>> array(-0.019486344729027664)

score.shape
>>> ()

score[0]
>>> IndexError: too many indices for array

如何从分数数组中提取双精度数?

How can I extract a double from the score array?

谢谢.

推荐答案

您可以通过索引[()]来访问0维数组内的数据.

You can access the data inside a 0-dimensional array via indexing [()].

例如,score[()]将检索数组中的基础数据.

For example, score[()] will retrieve the underlying data in your array.

这个习惯用法实际上是一致的:

The idiom is in fact consistent:

# x, y, z are 0-dim, 1-dim, 2-dim respectively
x = np.array(1)
y = np.array([1, 2, 3])
z = np.array([[1, 2, 3], [4, 5, 6]])

# use 0-dim, 1-dim, 2-dim tuple indexers respectively
res_x = x[()]      # 1
res_y = y[(1,)]    # 2
res_z = z[(1, 2)]  # 6

音色看起来不自然,因为您无需在1d和2d情况下显式使用它们,即y[1]z[1, 2]就足够了.该选项不适用于0-dim情况,因此请使用零长度元组.

Tuples seem unnatural because you don't need to use them explicitly for the 1d and 2d cases, i.e. y[1] and z[1, 2] suffice. That option isn't available for the 0-dim case, so use the zero-length tuple.

这篇关于ctypes-numpy数组没有形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆