python - 填充作为参数传递给 C 函数的 Python 列表

标签 python c python-2.7 python-c-api

我正在使用 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 to list.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). Return 0 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/

相关文章:

python - 索引 numpy 数组,其中每个列都有两个数组的开始和结束

python - 在 python 2.7 中处理来自 Mechanize 的异常

python - 如何通过 dev_appserver.py 在 Google App Engine 上使用 BeautifulSoup 和 lxml

python - 在Python中比较两个列表的子列表

c - 赋值中的类型不兼容。 C

python - 请将所有出现的 "import gobject"更改为 "from gi.repository import GObject"

c - C 中的对偶四元数实现

C 具有基本整数值的 float 给出不同的结果

python - 如何从 Python 2.7 中的 csv 读取数据中找到最大数量?

python - 类型错误 : "quotechar" must be an 1-character string