您可以使用PyList_New()
, PyTuple_New()
,PyList_Append()
和PyTuple_SetItem()
来完成此操作...
const Py_ssize_t tuple_length = 4;
const unsigned some_limit = 4;
PyObject *my_list = PyList_New(0);
if(my_list == NULL) {
// ...
}
for(unsigned i = 0; i < some_limit; i++) {
PyObject *the_tuple = PyTuple_New(tuple_length);
if(the_tuple == NULL) {
// ...
}
for(Py_ssize_t j = 0; i < tuple_length; i++) {
PyObject *the_object = PyLong_FromSsize_t(i * tuple_length + j);
if(the_object == NULL) {
// ...
}
PyTuple_SET_ITEM(the_tuple, j, the_object);
}
if(PyList_Append(my_list, the_tuple) == -1) {
// ...
}
}
这将创建一个表单列表...
[(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15)]