我正在使用 C 编写 Python 扩展。当列表作为参数传递并且 C 函数不返回列表而是返回不同的 PyObject*
时,如何在 C 中填充空列表.
b=[]
a = c.populate(data=b)
print b
输出:
[1, 2, 3]
感谢您的帮助。谢谢。
最佳答案
您可以使用PyList_Append()
function将单个元素添加到现有的 Python list
对象中:
Append the object item at the end of list list. Return
0
if successful; return-1
and set an exception if unsuccessful. Analogous tolist.append(item)
.
或者,使用 PyList_SetSlice()
使用另一个 list
对象中的项目扩展列表:
Set the slice of list between low and high to the contents of itemlist. Analogous to
list[low:high] = itemlist
. The itemlist may be NULL, indicating the assignment of an empty list (slice deletion). Return0
on success,-1
on failure. Negative indices, as when slicing from Python, are not supported.
关于python - 填充作为参数传递给 C 函数的 Python 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24248121/