1

我执行了这个python 脚本。行中发生错误

t = gdcm.Orientation.GetType(dircos)

错误信息是:

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2820, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
  File "<ipython-input-8-fb43b0929780>", line 1, in <module>
    gdcm.Orientation.GetType(dircos)
TypeError: expected a list.

我查了类参考。它说

输入是 6 个双精度数组

该变量dircos恰好是一个包含 6 个元素的列表,

>>> dircos
Out[11]: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]

我不知道为什么会出错。

4

1 回答 1

3

我检查了源代码,发现它实际上检查了tuple. 该消息具有误导性。

// Grab a 6 element array as a Python 6-tuple
%typemap(in) const double dircos[6] (double temp[6]) {   // temp[6] becomes a local variable
  int i;
  if (PyTuple_Check($input) /*|| PyList_Check($input)*/) {
    if (!PyArg_ParseTuple($input,"dddddd",temp,temp+1,temp+2,temp+3,temp+4,temp+5)) {
      PyErr_SetString(PyExc_TypeError,"list must have 6 elements");
      return NULL;
    }
    $1 = &temp[0];
  } else {
    PyErr_SetString(PyExc_TypeError,"expected a list.");
    return NULL;
  }
}

你需要传递一个元组:

>>> import gdcm
>>> dircos = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
>>> gdcm.Orientation.GetType(tuple(dircos))
1
于 2016-04-04T04:19:16.217 回答