列表對象?
-
PyTypeObject PyList_Type?
- Part of the Stable ABI.
這是個屬于
PyTypeObject
的代表Python列表類型的實例。在Python層面和類型list
是同一個對象。
-
PyObject *PyList_New(Py_ssize_t len)?
- Return value: New reference. Part of the Stable ABI.
成功時返回一個長度為 len 的新列表,失敗時返回
NULL
。備注
當 len 大于零時,被返回的列表對象項目被設成
NULL
。因此你不能用類似C函數(shù)PySequence_SetItem()
的抽象API或者用C函數(shù)PyList_SetItem()
將所有項目設置成真實對象前對Python代碼公開這個對象。
-
Py_ssize_t PyList_Size(PyObject *list)?
- Part of the Stable ABI.
返回 list 中列表對象的長度;這等于在列表對象調(diào)用
len(list)
。
-
Py_ssize_t PyList_GET_SIZE(PyObject *list)?
Similar to
PyList_Size()
, but without error checking.
-
PyObject *PyList_GetItem(PyObject *list, Py_ssize_t index)?
- Return value: Borrowed reference. Part of the Stable ABI.
返回 list 所指向列表中 index 位置上的對象。 位置值必須為非負數(shù);不支持從列表末尾進行索引。 如果 index 超出邊界 (<0 or >=len(list)),則返回
NULL
并設置IndexError
異常。
-
PyObject *PyList_GET_ITEM(PyObject *list, Py_ssize_t i)?
- Return value: Borrowed reference.
Similar to
PyList_GetItem()
, but without error checking.
-
int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)?
- Part of the Stable ABI.
將列表中索引為 index 的項設為 item。 成功時返回
0
。 如果 index 超出范圍則返回-1
并設定IndexError
異常。備注
此函數(shù)會“偷走”一個對 item 的引用并丟棄一個對列表中受影響位置上的已有條目的引用。
-
void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)?
不帶錯誤檢測的宏版本
PyList_SetItem()
。 這通常只被用于新列表中之前沒有內(nèi)容的位置進行填充。備注
該宏會“偷走”一個對 item 的引用,但與
PyList_SetItem()
不同的是它 不會 丟棄對任何被替換條目的引用;在 list 的 i 位置上的任何引用都將被泄露。
-
int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)?
- Part of the Stable ABI.
將條目 item 插入到列表 list 索引號 index 之前的位置。 如果成功將返回
0
;如果不成功則返回-1
并設置一個異常。 相當于list.insert(index, item)
。
-
int PyList_Append(PyObject *list, PyObject *item)?
- Part of the Stable ABI.
將對象 item 添加到列表 list 的末尾。 如果成功將返回
0
;如果不成功則返回-1
并設置一個異常。 相當于list.append(item)
。
-
PyObject *PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)?
- Return value: New reference. Part of the Stable ABI.
返回一個對象列表,包含 list 當中位于 low 和 high 之間 的對象。 如果不成功則返回
NULL
并設置異常。 相當于list[low:high]
。 不支持從列表末尾進行索引。
-
int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)?
- Part of the Stable ABI.
將 list 當中 low 與 high 之間的切片設為 itemlist 的內(nèi)容。 相當于
list[low:high] = itemlist
。 itemlist 可以為NULL
,表示賦值為一個空列表(刪除切片)。 成功時返回0
,失敗時返回-1
。 這里不支持從列表末尾進行索引。
-
int PyList_Sort(PyObject *list)?
- Part of the Stable ABI.
對 list 中的條目進行原地排序。 成功時返回
0
,失敗時返回-1
。 這等價于list.sort()
。
-
int PyList_Reverse(PyObject *list)?
- Part of the Stable ABI.
對 list 中的條目進行原地反轉(zhuǎn)。 成功時返回
0
,失敗時返回-1
。 這等價于list.reverse()
。
-
PyObject *PyList_AsTuple(PyObject *list)?
- Return value: New reference. Part of the Stable ABI.
返回一個新的元組對象,其中包含 list 的內(nèi)容;等價于
tuple(list)
。