1、实现代码
#创建stu_info = { "xiedi":28, "liuhailin":27,"daiqiao":30,"hanwenhai":25,"chenqun":38}print(stu_info)#增加stu_info["luoahong"]=32print(stu_info)#修改stu_info["xiedi"]=29print(stu_info)
输出结果
{'xiedi': 28, 'liuhailin': 27, 'daiqiao': 30, 'hanwenhai': 25, 'chenqun': 38}{'xiedi': 28, 'liuhailin': 27, 'daiqiao': 30, 'hanwenhai': 25, 'chenqun': 38, 'luoahong': 32}{'xiedi': 29, 'liuhailin': 27, 'daiqiao': 30, 'hanwenhai': 25, 'chenqun': 38, 'luoahong': 32}
1、实现代码
del stu_info["chenqun"]print(stu_info)
2、输出结果
{'xiedi': 29, 'liuhailin': 27, 'daiqiao': 30, 'hanwenhai': 25, 'luoahong': 32}
intPyDict_DelItem(PyObject *op, PyObject *key){ Py_hash_t hash; assert(key); if (!PyUnicode_CheckExact(key) || (hash = ((PyASCIIObject *) key)->hash) == -1) { hash = PyObject_Hash(key); if (hash == -1) return -1; } return _PyDict_DelItem_KnownHash(op, key, hash);}
int_PyDict_DelItem_KnownHash(PyObject *op, PyObject *key, Py_hash_t hash){ Py_ssize_t ix; PyDictObject *mp; PyObject *old_value; if (!PyDict_Check(op)) { PyErr_BadInternalCall(); return -1; } assert(key); assert(hash != -1); mp = (PyDictObject *)op; ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); if (ix == DKIX_ERROR) return -1; if (ix == DKIX_EMPTY || old_value == NULL) { _PyErr_SetKeyError(key); return -1; } // Split table doesn't allow deletion. Combine it. if (_PyDict_HasSplitTable(mp)) { if (dictresize(mp, DK_SIZE(mp->ma_keys))) { return -1; } ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); assert(ix >= 0); } return delitem_common(mp, hash, ix, old_value);}/* This function promises that the predicate -> deletion sequence is atomic * (i.e. protected by the GIL), assuming the predicate itself doesn't * release the GIL. */
int_PyDict_DelItemIf(PyObject *op, PyObject *key, int (*predicate)(PyObject *value)){ Py_ssize_t hashpos, ix; PyDictObject *mp; Py_hash_t hash; PyObject *old_value; int res; if (!PyDict_Check(op)) { PyErr_BadInternalCall(); return -1; } assert(key); hash = PyObject_Hash(key); if (hash == -1) return -1; mp = (PyDictObject *)op; ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); if (ix == DKIX_ERROR) return -1; if (ix == DKIX_EMPTY || old_value == NULL) { _PyErr_SetKeyError(key); return -1; } // Split table doesn't allow deletion. Combine it. if (_PyDict_HasSplitTable(mp)) { if (dictresize(mp, DK_SIZE(mp->ma_keys))) { return -1; } ix = (mp->ma_keys->dk_lookup)(mp, key, hash, &old_value); assert(ix >= 0); } res = predicate(old_value); if (res == -1) return -1; hashpos = lookdict_index(mp->ma_keys, hash, ix); assert(hashpos >= 0); if (res > 0) return delitem_common(mp, hashpos, ix, old_value); else return 0;}
新闻热点
疑难解答