新聞中心
字典對象
type PyDictObject

創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷推廣、網(wǎng)站重做改版、繁昌網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、成都商城網(wǎng)站開發(fā)、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為繁昌等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
這個 PyObject 的子類型代表一個python字典對象。
PyTypeObject PyDict_Type
Part of the Stable ABI.
Python字典類型表示為 PyTypeObject 的實例。這與Python層面的 dict 是相同的對象。
int PyDict_Check(PyObject *p)
如果 p 是一個 dict 對象或者 dict 類型的子類型的實例則返回真值。 此函數(shù)總是會成功執(zhí)行。
int PyDict_CheckExact(PyObject *p)
如果 p 是一個 dict 對象但不是 dict 類型的子類型的實例則返回真值。 此函數(shù)總是會成功執(zhí)行。
PyObject *PyDict_New()
Return value: New reference. Part of the Stable ABI.
返回一個新的空字典,失敗時返回 NULL。
PyObject *PyDictProxy_New(PyObject *mapping)
Return value: New reference. Part of the Stable ABI.
返回 types.MappingProxyType 對象,用于強制執(zhí)行只讀行為的映射。這通常用于創(chuàng)建視圖以防止修改非動態(tài)類類型的字典。
void PyDict_Clear(PyObject *p)
Part of the Stable ABI.
清空現(xiàn)有字典的所有鍵值對。
int PyDict_Contains(PyObject *p, PyObject *key)
Part of the Stable ABI.
確定 key 是否包含在字典 p 中。如果 key 匹配上 p 的某一項,則返回 1 ,否則返回 0 。返回 -1 表示出錯。這等同于Python表達式 key in p 。
PyObject *PyDict_Copy(PyObject *p)
Return value: New reference. Part of the Stable ABI.
返回與 p 包含相同鍵值對的新字典。
int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)
Part of the Stable ABI.
使用 key 作為鍵將 val 插入字典 p。 key 必須為 hashable;如果不是,則將引發(fā) TypeError。 成功時返回 0,失敗時返回 -1。 此函數(shù) 不會 附帶對 val 的引用。
int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)
Part of the Stable ABI.
Insert val into the dictionary p using key as a key. key should be a const char*. The key object is created using PyUnicode_FromString(key). Return 0 on success or -1 on failure. This function does not steal a reference to val.
int PyDict_DelItem(PyObject *p, PyObject *key)
Part of the Stable ABI.
移除字典 p 中鍵為 key 的條目。 key 必須是可哈希的;如果不是,則會引發(fā) TypeError。 如果字典中沒有 key,則會引發(fā) KeyError。 成功時返回 0,失敗時返回 -1。
int PyDict_DelItemString(PyObject *p, const char *key)
Part of the Stable ABI.
移除字典 p 中由字符串 key 指定的鍵的條目。 如果字典中沒有 key,則會引發(fā) KeyError。 成功時返回 0,失敗時返回 -1。
PyObject *PyDict_GetItem(PyObject *p, PyObject *key)
Return value: Borrowed reference. Part of the Stable ABI.
從字典 p 中返回以 key 為鍵的對象。 如果鍵名 key 不存在但 沒有 設(shè)置一個異常則返回 NULL。
需要注意的是,調(diào)用 __hash__() 和 __eq__() 方法產(chǎn)生的異常不會被拋出。改用 PyDict_GetItemWithError() 獲得錯誤報告。
在 3.10 版更改: 在不保持 GIL 的情況下調(diào)用此 API 曾因歷史原因而被允許。 現(xiàn)在已不再被允許。
PyObject *PyDict_GetItemWithError(PyObject *p, PyObject *key)
Return value: Borrowed reference. Part of the Stable ABI.
PyDict_GetItem() 的變種,它不會屏蔽異常。 當異常發(fā)生時將返回 NULL 并且 設(shè)置一個異常。 如果鍵不存在則返回 NULL 并且不會 設(shè)置一個異常。
PyObject *PyDict_GetItemString(PyObject *p, const char *key)
Return value: Borrowed reference. Part of the Stable ABI.
This is the same as PyDict_GetItem(), but key is specified as a const char*, rather than a PyObject*.
需要注意的是,調(diào)用 __hash__() 、 __eq__() 方法和創(chuàng)建一個臨時的字符串對象時產(chǎn)生的異常不會被拋出。改用 PyDict_GetItemWithError() 獲得錯誤報告。
PyObject *PyDict_SetDefault(PyObject *p, PyObject *key, PyObject *defaultobj)
Return value: Borrowed reference.
這跟Python層面的 dict.setdefault() 一樣。如果鍵 key 存在,它返回在字典 p 里面對應(yīng)的值。如果鍵不存在,它會和值 defaultobj 一起插入并返回 defaultobj 。這個函數(shù)只計算 key 的哈希函數(shù)一次,而不是在查找和插入時分別計算它。
3.4 新版功能.
PyObject *PyDict_Items(PyObject *p)
Return value: New reference. Part of the Stable ABI.
返回一個包含字典中所有鍵值項的 PyListObject。
PyObject *PyDict_Keys(PyObject *p)
Return value: New reference. Part of the Stable ABI.
返回一個包含字典中所有鍵(keys)的 PyListObject。
PyObject *PyDict_Values(PyObject *p)
Return value: New reference. Part of the Stable ABI.
返回一個包含字典中所有值(values)的 PyListObject。
Py_ssize_t PyDict_Size(PyObject *p)
Part of the Stable ABI.
返回字典中項目數(shù),等價于對字典 p 使用 len(p)。
int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
Part of the Stable ABI.
Iterate over all key-value pairs in the dictionary p. The Py_ssize_t referred to by ppos must be initialized to 0 prior to the first call to this function to start the iteration; the function returns true for each pair in the dictionary, and false once all pairs have been reported. The parameters pkey and pvalue should either point to PyObject* variables that will be filled in with each key and value, respectively, or may be NULL. Any references returned through them are borrowed. ppos should not be altered during iteration. Its value represents offsets within the internal dictionary structure, and since the structure is sparse, the offsets are not consecutive.
例如:
PyObject *key, *value;Py_ssize_t pos = 0;while (PyDict_Next(self->dict, &pos, &key, &value)) {/* do something interesting with the values... */...}
字典 p 不應(yīng)該在遍歷期間發(fā)生改變。在遍歷字典時,改變鍵中的值是安全的,但僅限于鍵的集合不發(fā)生改變。例如:
PyObject *key, *value;Py_ssize_t pos = 0;while (PyDict_Next(self->dict, &pos, &key, &value)) {long i = PyLong_AsLong(value);if (i == -1 && PyErr_Occurred()) {return -1;}PyObject *o = PyLong_FromLong(i + 1);if (o == NULL)return -1;if (PyDict_SetItem(self->dict, key, o) < 0) {Py_DECREF(o);return -1;}Py_DECREF(o);}
int PyDict_Merge(PyObject *a, PyObject *b, int override)
Part of the Stable ABI.
對映射對象 b 進行迭代,將鍵值對添加到字典 a。 b 可以是一個字典,或任何支持 PyMapping_Keys() 和 PyObject_GetItem() 的對象。 如果 override 為真值,則如果在 b 中找到相同的鍵則 a 中已存在的相應(yīng)鍵值對將被替換,否則如果在 a 中沒有相同的鍵則只是添加鍵值對。 當成功時返回 0 或者當引發(fā)異常時返回 -1。
int PyDict_Update(PyObject *a, PyObject *b)
Part of the Stable ABI.
這與 C 中的 PyDict_Merge(a, b, 1) 一樣,也類似于 Python 中的 a.update(b),差別在于 PyDict_Update() 在第二個參數(shù)沒有 “keys” 屬性時不會回退到迭代鍵值對的序列。 當成功時返回 0 或者當引發(fā)異常時返回 -1。
int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)
Part of the Stable ABI.
將 seq2 中的鍵值對更新或合并到字典 a。 seq2 必須為產(chǎn)生長度為 2 的用作鍵值對的元素的可迭代對象。 當存在重復(fù)的鍵時,如果 override 真值則最后出現(xiàn)的鍵勝出。 當成功時返回 0 或者當引發(fā)異常時返回 -1。 等價的 Python 代碼(返回值除外):
def PyDict_MergeFromSeq2(a, seq2, override):for key, value in seq2:if override or key not in a:a[key] = value
當前文章:創(chuàng)新互聯(lián)Python教程:字典對象
標題來源:http://fisionsoft.com.cn/article/dhsocgh.html


咨詢
建站咨詢
