首页 > 编程 > Python > 正文

Python字典添加,删除,查询等相关操作方法详解

2020-02-15 21:19:41
字体:
来源:转载
供稿:网友

一、创建增加修改

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}

二、删除(del)

1、实现代码

del stu_info["chenqun"]print(stu_info)

2、输出结果

{'xiedi': 29, 'liuhailin': 27, 'daiqiao': 30, 'hanwenhai': 25, 'luoahong': 32}

 1、Dict_DelItem 

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);}

2、Dict_DelItem_KnownHash

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. */

3、PyDict_DelItemIf

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;}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表